1.如果是在微信小程序中使用导航,需要使用腾讯地图线路规划插件,
getLocation() {
let that = this
// 获取用户是否开启 授权获取当前的地理位置、速度的权限。
uni.getSetting({
success(res) {
console.log(res)
// 如果没有授权
if (!res.authSetting['scope.userLocation']) {
// 则拉起授权窗口
uni.authorize({
scope: 'scope.userLocation',
success() {
//点击允许后--就一直会进入成功授权的回调 就可以使用获取的方法了
uni.getLocation({
type: 'gcj02',
success: res => {
// 获取到坐标后存起来
that.latitude = res.latitude
that.longitude = res.longitude
console.log(res, 0)
// 使用地图实例中的方法,让标记和我的位置都在可视区域里显示
that.mapCtx.includePoints({
padding: [140, 50, 140, 50],
points: [
// 第一个是我的位置数据
{
latitude: that.latitude,
longitude: that.longitude
},
// 第二个是目标位置数据
{
latitude: that.markers[0]
.latitude,
longitude: that.markers[0]
.longitude
}
]
})
},
fail: err => {
console.log(1)
}
})
},
fail(error) {
//点击了拒绝授权后--就一直会进入失败回调函数--此时就可以在这里重新拉起授权窗口
console.log('拒绝授权', error)
uni.showModal({
title: '提示',
content: '若点击不授权,将无法使用位置功能',
cancelText: '不授权',
cancelColor: '#999',
confirmText: '授权',
confirmColor: '#f94218',
success(res) {
console.log(res)
if (res.confirm) {
// 选择弹框内授权
uni.openSetting({
success(res) {
console.log(res.authSetting)
}
})
} else if (res.cancel) {
// 选择弹框内 不授权
console.log('用户点击不授权')
}
}
})
}
})
} else {
// 有权限则直接获取
uni.getLocation({
type: 'gcj02',
success: res => {
// 获取到坐标后存起来
that.latitude = res.latitude
that.longitude = res.longitude
console.log(res, 0)
// 使用地图实例中的方法,让标记和我的位置都在可视区域里显示
that.mapCtx.includePoints({
padding: [140, 50, 140, 50],
points: [
// 第一个是我的位置数据
{
latitude: that.latitude,
longitude: that.longitude
},
// 第二个是目标位置数据
{
latitude: that.markers[0].latitude,
longitude: that.markers[0].longitude
}
]
})
},
fail: err => {
console.log(err)
}
})
}
}
})
},
2.直接调用外部地图
uni.openLocation({
latitude: Number(this.lat),
longitude: Number(this.log),
name:this.info.hospitalName
})
传入目的地经纬度 进行选择外部地图。