112 lines
3.6 KiB
JavaScript
112 lines
3.6 KiB
JavaScript
import config from '@/utils/mapConfig';
|
|
|
|
module.exports = {
|
|
getLocation() {
|
|
return uni.getLocation({
|
|
type: 'wgs84'
|
|
})
|
|
},
|
|
getLocationPromise() {
|
|
return new Promise((resolve, reject) => {
|
|
uni.getLocation({
|
|
type: 'wgs84',
|
|
success: (res) => {
|
|
resolve(res)
|
|
},
|
|
fail: () => {
|
|
resolve({})
|
|
}
|
|
})
|
|
})
|
|
},
|
|
|
|
getCurAddress(latitude, longitude, cb) {
|
|
const postStr = {
|
|
lon: longitude,
|
|
lat: latitude,
|
|
ver: 1 // 接口版本号
|
|
}
|
|
const encodedPostStr = encodeURIComponent(JSON.stringify(postStr))
|
|
|
|
uni.request({
|
|
url: `https://api.tianditu.gov.cn/geocoder?postStr=${encodedPostStr}&type=geocode&tk=${config.key}`,
|
|
method: 'GET',
|
|
success: (res) => {
|
|
if (res.data.status === '0') { // 请求成功[6](@ref)
|
|
const result = res.data.result;
|
|
// 从返回数据中提取省市区信息[1,6](@ref)
|
|
const addressComponent = result.addressComponent;
|
|
const province = addressComponent.province; // 省份
|
|
const city = addressComponent.city; // 城市
|
|
const county = addressComponent.county; // 区县
|
|
const formattedAddress = result.formatted_address; // 完整地址
|
|
cb && cb({
|
|
province,
|
|
city,
|
|
county,
|
|
formattedAddress
|
|
})
|
|
}
|
|
uni.hideLoading()
|
|
},
|
|
fail: () => {
|
|
uni.hideLoading()
|
|
}
|
|
})
|
|
},
|
|
|
|
getLocationSetting(cb) {
|
|
uni.getSetting({
|
|
success: res => {
|
|
if (!res.authSetting['scope.userLocation']) {
|
|
uni.showModal({
|
|
title: '提示',
|
|
content: '当前定位未开启,请点击确定手动开启定位',
|
|
success: response => {
|
|
if (response.confirm) {
|
|
cb && cb ()
|
|
} else {
|
|
uni.showToast({
|
|
title: '您拒绝了授权,无法获取定位服务',
|
|
duration: 2000,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
openLocationSettting() {
|
|
return new Promise((resolve, reject) => {
|
|
uni.openSetting({
|
|
success: res => {
|
|
if (res.authSetting['scope.userLocation']) {
|
|
resolve()
|
|
} else {
|
|
uni.showToast({
|
|
title: '您拒绝了授权,无法获取定位服务',
|
|
duration: 2000,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
fail: () => {
|
|
reject()
|
|
}
|
|
})
|
|
})
|
|
},
|
|
|
|
getUrlParam(url, name) {
|
|
url = decodeURIComponent(url)
|
|
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
|
|
var r = url.match(reg);
|
|
console.log(r);
|
|
if (r != null) return (r[2]);
|
|
return null;
|
|
}
|
|
}
|