微信小程序-获取当前位置

在 app.json 里面增加 permission 属性配置(小游戏需在game.json中配置):

"permission": {
    "scope.userLocation": {
      "desc": "您的位置信息将用于地图展示"
    }
  }

index.wxml:

<view>
  <!--点击跳转到地图-->
  <button bindtap="map">定位</button>
  <!--直接打开地图-->
  <map id="map" scale="14" show-location style="width:100%; height: 500px;"></map>
</view>

index.js:

Page({
  data: {

  },
  map: function () {
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28
        })
      }
    })
  }
})

 

posted @ 2019-12-11 09:46  shirliey  阅读(944)  评论(0编辑  收藏  举报