uniApp gets the latitude and longitude of the current location

Here is sample code to get the current location using uni.getLocation :

  1. Call the uni.getLocation method to obtain the current location information
    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);
      }
    });

    If you need to obtain location information continuously, you can use the uni.startLocationUpdate method

    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);
      }
    });

    It should be noted that obtaining location information requires user authorization. Without authorization, location information cannot be obtained. If you need to obtain location information, please add the following permissions in the manifest.json file:

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

Guess you like

Origin blog.csdn.net/m0_71966801/article/details/135433900