前端使用腾讯地图api实现定位功能
1.配置key
申请地址: https://lbs.qq.com/dev/console/key/manage
2.在项目中引入jssdk
<script type="text/javascript" src="https://apis.map.qq.com/tools/geolocation/min?key=key&referer=myapp"></script>
使用
const geolocation = new (window as any).qq.maps.Geolocation("key", "myapp");
const that = this
// 定义定位成功回调函数
function showPosition(position) {}
// 定义定位失败回调函数
function showError(error) {// 提示用户定位失败uni.showToast({title: "定位失败",icon: "none",})
}
// 发起定位请求
geolocation.getLocation(showPosition, showError, {options: {timeout: 10000}})
腾讯地图提供了三种调用方式,我用的是第三种,如果想试试其他两种的小伙伴可以自己研读一下哦,附链接地址
https://lbs.qq.com/webApi/component/componentGuide/componentGeolocation