uniApp获取当前位置经纬度

以下是使用uni.getLocation获取当前位置的示例代码:

  1. 调用uni.getLocation方法获取当前位置信息
uni.getLocation({
  type: 'wgs84', // 坐标类型,默认为wgs84,可选的值为gcj02和bd09ll
  success: res => {
    // 获取成功,经度和纬度在res.longitude和res.latitude中
    console.log('longitude:', res.longitude);
    console.log('latitude:', res.latitude);
  },
  fail: err => {
    // 获取失败,err为错误信息
    console.log('getLocation err:', err);
  }
});

  1. 如果需要连续获取位置信息,可以使用uni.startLocationUpdate方法
uni.startLocationUpdate({
  accuracy: 'high', // 定位精度,可选值为low、medium、high,默认为high
  autoStop: false, // 是否自动停止位置更新,默认为false
  success: res => {
    console.log('longitude:', res.longitude);
    console.log('latitude:', res.latitude);
  },
  fail: err => {
    console.log('startLocationUpdate err:', err);
  }
});

需要注意的是,获取位置信息需要用户授权,如果没有授权,则无法获取位置信息。如果需要获取位置信息,请在manifest.json文件中添加以下权限:

"permissions": {
  "location": {
    "desc": "您的位置信息将用于获取您周边的优惠信息"
  }
}

uniApp获取当前位置经纬度_第1张图片

你可能感兴趣的:(uniapp,uni-app,javascript,前端)