问题描述:微信公众号h5项目,使用微信jssdk获取经纬度,使用腾讯地图解析,苹果手机定位功能正常,安卓手机定位失败
使用微信jsdk方法调取经纬度信息 配置为type:gcj02 ,
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
var speed = res.speed; // 速度,以米/每秒计
var accuracy = res.accuracy; // 位置精度
}
});
//获取定位经纬度
if (this.$wechat && this.$wechat.isWechat()) {
console.log('phoneList获取地理信息');
this.$wechat.location((res) => {
//苹果手机返回经纬度为数字,安卓手机为字符串
console.log('微信返回地理信息', res)
// let latitude = (res.latitude).toFixed(6); // 纬度,浮点数,范围为90
// let longitude = (res.longitude).toFixed(6); // 经度,浮点数,范围为
let latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
let longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
console.log('处理经纬度-----',typeof(latitude));
this.httpgz({//封装的请求方法
url : '后端调腾讯地图处理经纬度接口',
method:'get',
data:{latitude,longitude},
success: res => {
if (res.code == 200){
console.log('获取地址',res);
}
}
})
})
};
反复测试排查,得出推断的结论,苹果手机调wx.getLocation 返回经纬度信息为number类型,安卓手机返回经纬度为string类型,导致我使用的toFixed()方法报错
删除toFixed()方法,解决问题