微信小程序获取地图中心坐标方法

微信小程序可以通过 wx.selectLocation API 获取用户选择的位置信息,包括经纬度坐标。

使用方法:

  1. 调用 wx.selectLocation API:
wx.selectLocation({
  success: (res) => {
    console.log(res.latitude, res.longitude); // 获取经纬度坐标
  }
});
  1. 获取经纬度坐标:success 回调函数中,res.latituderes.longitude 分别代表经度和纬度坐标。

注意:

  • 用户需要授权小程序访问位置信息,才能使用 wx.selectLocation API。
  • wx.checkLocation 用于检查用户是否授权小程序访问位置信息。
  • wx.findLocation 用于根据地址信息获取地理位置坐标,不适用于获取地图中心坐标。
  • getCenterLocation(OBJECT) 不是微信小程序提供的 API,无法使用。

示例:

wx.checkLocation({
  success: (res) => {
    if (res.authStatus === 'authorized') { // 用户已授权
      wx.selectLocation({
        success: (res) => {
          console.log('获取到的经纬度坐标:', res.latitude, res.longitude);
        }
      });
    } else { // 用户未授权
      wx.authorize({
        scope: 'scope.userLocation',
        success: () => {
          wx.selectLocation({
            success: (res) => {
              console.log('获取到的经纬度坐标:', res.latitude, res.longitude);
            }
          });
        }
      });
    } 
  }
});
微信小程序获取地图中心坐标方法

原文地址: https://www.cveoy.top/t/topic/nOsP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录