谷歌地图 nvue moveToLocation移动地图到指定位置,h5和ios正常,安卓偶尔成功 大部分失败
谷歌地图 nvue moveToLocation移动地图到指定位置,h5和ios正常,安卓偶尔成功 大部分失败
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | Windows11 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
PC开发环境操作系统版本号:windows11
HBuilderX类型:正式
HBuilderX版本号:3.8.12
手机系统:Android
手机系统版本号:Android 13
手机厂商:华为
手机机型:iqoo8
页面类型:vue
vue版本:vue2
打包方式:云端
App下载地址或H5网址:https://www.pgyer.com/iFWHe9
示例代码:
//地图
<map :show-location=“true” id=“myMap” :latitude=“staParam.userLat” :longitude=“staParam.userLng”
:markers=“markers” @markertap=“markertap” class=“my-map”>
</map>
// 按钮
<view class="map-right">
<view class=“map-right-item” @click=“moveLocation()”>
<image class="item-img" src="../../static/img/union.png"></image>
</view>
</view>
// js
onReady(){
this.mapContext = uni.createMapContext(‘myMap’,this);
}
moveLocation () {
uni.getLocation({
success: (ret) => {
this.staParam.userLat = parseFloat(ret.latitude);
this.staParam.userLng = parseFloat(ret.longitude);
this.mapContext = uni.createMapContext(‘myMap’)
this.mapContext.moveToLocation({
latitude: parseFloat(ret.longitude),
longitude: parseFloat(ret.longitude),
success: (res) => {
console.log(res);
},
fail: (error) => {
console.log(error);
}
},
});
}
请问解决了吗? 我现在也遇到同样的问题了?
这是一个关于uni-app中使用map组件moveToLocation方法在Android平台表现不一致的问题。根据代码分析,问题可能出在以下几个方面:
- 代码中存在明显的经纬度参数错误:
latitude: parseFloat(ret.longitude),
longitude: parseFloat(ret.longitude)
这里两个参数都使用了longitude值,应该分别使用latitude和longitude。
-
Android平台对地图API的调用可能需要额外的权限处理,建议检查是否已获取所有必要权限。
-
建议修改后的代码:
moveLocation() {
uni.getLocation({
success: (ret) => {
this.staParam.userLat = parseFloat(ret.latitude);
this.staParam.userLng = parseFloat(ret.longitude);
this.mapContext.moveToLocation({
latitude: parseFloat(ret.latitude),
longitude: parseFloat(ret.longitude),
success: (res) => {
console.log('移动成功', res);
},
fail: (error) => {
console.log('移动失败', error);
}
});
}
});
}