import Vue from 'vue'
import QQMapWX from './libs/qqmap-wx-jssdk.js'
// 安全域名设置,在小程序管理后台 设置request合法域名,添加https://apis.map.qq.com
// 开启webservice 域名白名单留空=不限制
const qqMap = Vue.prototype.$qqMap = new QQMapWX({
key: 'MOYBZ-XXXXXXXXXXXXXXXXX-6-YYFJ4',
})
export const getLocation = Vue.prototype.$getLocation = () => {
return new Promise((resolve, reject) => {
// if(1) {
// setTimeout(() => {
// reject(new Error('test err'))
// }, 1000);
// return
// }
// 定位
uni.getLocation({
type: 'gcj02',
success({
latitude,
longitude
}) {
resolve({
location: {
latitude,
longitude,
},
})
},
fail: reject,
})
})
}
export const getGeoInfo = Vue.prototype.$getGeoInfo = async (location) => {
if (!location) {
const res = await getLocation()
location = res.location
}
// 当前位置-经纬度
return new Promise((resolve, reject) => {
qqMap.reverseGeocoder({
location,
// 返回经纬度 res.result
success(res) {
resolve(res.result)
},
fail: reject,
})
})
}
// 根据经纬度获取地址
Vue.prototype.$openLocation = function(detail, scale = 15) {
const { lat, lon, address } = detail
let lats = lat,
lons = lon,
name = address;
uni.openLocation({
latitude: parseFloat(lats),
longitude: parseFloat(lons),
name,
scale,
success: function() {
console.log('获取地理位置success');
},
fail: function(err){
console.log('获取地理位置失败',err)
},
complete: function(val){
console.log(val)
}
})
}
// 计算距离
export const getNiceDistance = Vue.prototype.$getNiceDistance = function(meter) {
if (!meter) return '0m'
if (meter > 1e3) return (meter / 1e3).toFixed(1) + 'km'
return meter.toFixed(0) + 'm'
}
// 获取定位距离
export const getDistance = Vue.prototype.$getDistance = function(pos1, pos2, isRaw) {
let val = qqMap.getUtils().getDistance(pos1.lat, pos1.lng, pos2.lat, pos2.lng)
console.log(isRaw)
if (isRaw) return val
return getNiceDistance(val)
}
uni 获取地理位置公共方法
最新推荐文章于 2024-06-12 16:37:42 发布