已解决
uniapp中地图定位功能实现的几种方案
来自网友在路上 188888提问 提问时间:2023-11-06 13:06:41阅读次数: 88
最佳答案 问答题库888位专家为你答疑解惑
1.uniapp自带uni.getLocation
uni.getLocation(options) @getlocation | uni-app官网
实现思路:uni.getLocation获取经纬度后调用接口获取城市名
优点:方便快捷,直接调用
缺点:关闭定位后延时很久,无法控制定位延迟时间,流程卡顿。获取手机定位权限方法不支持h5
2.百度map
创建BMap.js
export default {init() {const BMap_URL = `https://api.map.baidu.com/api?ak=${ baiduAk }&s=1&callback=onBMapCallback`return new Promise((reslove, reject)=> {if(typeof BMap !== 'undefined') {reslove(BMap)}window.onBMapCallback = function() {reslove(BMap)}let scriptNode = document.createElement('script')scriptNode.setAttribute('type', 'text/javascript')scriptNode.setAttribute('src', BMap_URL)document.body,appendChild(scriptNode)})}
}
引入使用:
import map from '@/pages/plugins/BMap.js'
map.init().then(BMap=> {const locationCity = new BMap.Geolocation()locationCity.getCurrentPosition((options)=> {let city = options.address.cityif(!city) {city = '北京'}// store.commit('getlocation', city)Vue.prototype.$cityName = city// 挂载页面}).catch((e)=> {})
})
优点:比较稳健,功能支持群面
缺点:dom拼接百度map降低性能,初始化时慢3秒
3.浏览器内置对象navigator.geolocation
if(navigator.geolocation) {let options = {enableHighAccuracy: true, // 默认false, 为true时使用精准定位timeout: 5000, // 获取位置最长等待时间,默认不限时间maximumAge: 21600000, // 重复获取位置时,多长时间再次获取定位, 这里设置成6h 21600000}navigator.geolocation.getCurrentPosition(sucCallback, errorCallback, options)
}
function sucCallback(position) {var cords = position.coordsgetAddressInfo({ }).then(res=> {let city = res.data.cityif(!city) {city = '北京'}// store.commit('getlocation', city)Vue.prototype.$cityName = city// 挂载页面})
}
function errorCallback(error) {var err = error.codeswitch(err) {case 0: alert('未识别到位置信息')break;case 1: alert('您拒绝了定位权限')break;case 2: alert('地理位置获取失败')break;case 3:alert('定位超市')break;default://}// store.commit('getlocation', '北京')Vue.prototype.$cityName = '北京'// 挂载页面
}
优点:性能好加载快
缺点:仅支持https协议链接,http协议下无法正常定位
查看全文
99%的人还看了
相似问题
- 【LSTM】北京pm2.5 天气预测--pytorch版本,有代码可以跑通-LSTM回归问题,工程落地一网打尽
- “可信区块链运行监测服务平台TBM发展研讨会”将于11月23日在北京召开
- 2024北京国际艾灸/艾草/艾灸设备展会,北京中医健康展览会
- 文本生成高精准3D模型,北京智源AI研究院等出品—3D-GPT
- 启用NTP服务解决Linux系统时间与北京时间不同步问题
- 从用户角度出发,如何优化大数据可视化体验|北京蓝蓝UI设计公司
- TiDB x 北京银行丨新一代分布式数据库的探索与实践
- 程序设计与算法(二)算法基础(北京大学MOOC)
- 北京大学王选所推出 ALCUNA ,大模型新知识理解能力评测再添一榜!
- 2024北京智慧养老展/北京老年科技产品展/北京养老产业展会
猜你感兴趣
版权申明
本文"uniapp中地图定位功能实现的几种方案":http://eshow365.cn/6-33620-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!