云端建站快车,网站搭建交流群,网站设计模板 psd,华为云wordpress淘宝以下是使用uni.getLocation获取当前位置的示例代码#xff1a;
调用uni.getLocation方法获取当前位置信息
uni.getLocation({type: wgs84, // 坐标类型#xff0c;默认为wgs84#xff0c;可选的值为gcj02和bd09llsuccess: res {// 获取成功#xff0c;经度和纬度在r…以下是使用uni.getLocation获取当前位置的示例代码
调用uni.getLocation方法获取当前位置信息
uni.getLocation({type: wgs84, // 坐标类型默认为wgs84可选的值为gcj02和bd09llsuccess: res {// 获取成功经度和纬度在res.longitude和res.latitude中console.log(longitude:, res.longitude);console.log(latitude:, res.latitude);},fail: err {// 获取失败err为错误信息console.log(getLocation err:, err);}
});如果需要连续获取位置信息可以使用uni.startLocationUpdate方法
uni.startLocationUpdate({accuracy: high, // 定位精度可选值为low、medium、high默认为highautoStop: false, // 是否自动停止位置更新默认为falsesuccess: res {console.log(longitude:, res.longitude);console.log(latitude:, res.latitude);},fail: err {console.log(startLocationUpdate err:, err);}
});需要注意的是获取位置信息需要用户授权如果没有授权则无法获取位置信息。如果需要获取位置信息请在manifest.json文件中添加以下权限
permissions: {location: {desc: 您的位置信息将用于获取您周边的优惠信息}
}