微信小程序获取当前位置 地图定位导航-详细代码

小程序获取当前位置,回到当前位置,地图定位,导航

效果

因为小程序更新了获取地理位置API接口,需要先在app.json中配置一下permission字段 ,不然会报微信小程序getLocation 需要在app.json中声明permission字段

app.json:   (不知道具体位置可以看这里,这里有整个app.json的配置)

 
 "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  }

wxml:

<!--pages/map/map.wxml-->

<!-- 这是地图部分 -->

<view class="map_container">
  <map class='map' longitude='{{longitude}}' latitude='{{latitude}}' scale='{{scale}}' markers='{{markers}}' controls="{{controls}}" bindcontroltap="bindcontroltap" polyline='{{polyline}}' circles="{{circles}}" bindmarkertap='bindmarkertap' bindcontroltap='bindcontroltap'
    show-location></map>
</view>


<!-- 以下是导航部分 -->

<view class='list-guide'>
<!-- 这里的坐标本应该是从服务器获取数据的,这时丈先写死在页面上了 -->
  <view bindtap="onGuideTap" data-latitude='39.92392' data-longitude='116.411885' data-bankName='最高人民检察院'>
    <image src='/images/banklist/daohang.png' class='list-guide-imgae'></image>
    <text class='list-guide-text'>导航</text>
  </view>
  <view bindtap='onbankTap' data-bankId="{{item.BANK_ID}}">
    <image src='/images/banklist/xiangqing.png' class='list-guide-imgae'></image>
    <text class='list-guide-text'>详情</text>
  </view>

</view>

宽度不是满屏,所以加个样式

wxss:

/* pages/map/map.wxss */
.map_container {
  height: 260px;
  width: 100%;
}

.map {
  height: 100%;
  width: 100%;
}




.list-guide{
  display: flex;  
  flex-direction: row; 
  justify-content:space-around;
  border-top: 1px solid #ededed;
  height: 80rpx;
}
.list-guide-imgae{
  height: 70rpx;
  width: 70rpx;
  margin-right: 20px;
  vertical-align: middle;
}
.list-guide-text{
  vertical-align: middle;
  line-height: 90rpx;
  font-size: 35rpx;
}

下面就是最重要的JS部分了()

JS:

// pages/map/map.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addmissage: '选的位置',
    // markers	 Array	标记点
    stitle:'故宫',
    latitude: "",
    longitude: "",
    scale: 14,
    markers: [],
    //controls控件 是左下角圆圈小图标,用户无论放大多少,点这里可以立刻回到当前定位(控件(更新一下,即将废弃,建议使用 cover-view 代替))
    controls: [{
      id: 1,
      iconPath: '../../images/img/controls.png',
      position: {
        left: 15,
        top: 260 - 50,
        width: 40,
        height: 40
      },
      clickable: true
    }],
    distanceArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //获取当前的地理位置、速度
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        //赋值经纬度
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,

        })
      }
    })



  },
  //controls控件的点击事件
  bindcontroltap(e) {
    var that = this;
    if (e.controlId == 1) {
      that.setData({
        latitude: this.data.latitude,
        longitude: this.data.longitude,
        scale: 14,
      })
    }
  },
  //导航
  onGuideTap: function (event) {
    var lat = Number(event.currentTarget.dataset.latitude);
    var lon = Number(event.currentTarget.dataset.longitude);
    var bankName = event.currentTarget.dataset.bankname;
    console.log(lat);
    console.log(lon);
    wx.openLocation({
      type: 'gcj02',
      latitude: lat,
      longitude: lon,
      name: bankName,
      scale: 28
    })
  },



})

项目下载在这里

(只是地图部份,也有删减,非实际项目全部) 已更新

下面是我实际项目中的截图

  • 75
    点赞
  • 452
    收藏
    觉得还不错? 一键收藏
  • 113
    评论
获取微信小程序当前位置详细信息,你使用微信小程序提供的`wx.getLocation接口获取经纬度,然后使用地理逆编码服务将经度转换为详细地址信息。以下是示例代码: ```javascript // 在小程序的页面中调用获取位置的方法 wx.getLocation({ type: 'wgs84', // 返回经纬度信息的类型,这里使用wgs84 success: function(res) { var latitude = res.latitude; // 纬度 var longitude = res.longitude; // 经度 // 调用腾讯地图的逆地址解析接口,将经纬度转换为详细地址 wx.request({ url: 'https://apis.map.qq.com/ws/geocoder/v1', data: { location: latitude + ',' + longitude, key: 'your_tencent_map_api_key', get_poi: 1 }, success: function(res) { var address = res.data.result.address; // 详细地址信息 var pois = res.data.result.pois; // 附近的POI信息 // 在这里处理获取到的位置信息 console.log("详细地址:" + address); console.log("附近POI:" + JSON.stringify(pois)); }, fail: function(res) { // 处理失败情况 console.log("逆地址解析失败:" + res.errMsg); } }); }, fail: function(res) { // 处理失败情况 console.log("获取位置失败:" + res.errMsg); } }); ``` 在成功回调函数中,我们发送了一个HTTP请求给腾讯地图的逆地址解析接口,将获取到的经纬度信息传递给接口,同时提供你的腾讯地图API密钥。接口会返回详细的地址信息和附近的POI(兴趣点)信息。你可以根据需求处理这些信息。 请注意,为了使用腾讯地图的服务,你需要在小程序后台设置中申请并获取到腾讯地图API密钥,并将其替换到示例代码中的`your_tencent_map_api_key`处。 希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 113
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值