|
@ -31,154 +31,8 @@ function calculateDistance(lat1, lon1, lat2, lon2, fixed = 0) { //计算两点 |
|
|
return distance.toFixed(fixed) |
|
|
return distance.toFixed(fixed) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function getLocation(fn) { //获取用户经纬度
|
|
|
|
|
|
wxGetLocation() //此方法只用于提示用户打开gps
|
|
|
|
|
|
uni.getLocation({ |
|
|
|
|
|
type: 'gcj02', |
|
|
|
|
|
isHighAccuracy: true, |
|
|
|
|
|
highAccuracyExpireTime: 1000, |
|
|
|
|
|
success: function(position) { |
|
|
|
|
|
fn(position) |
|
|
|
|
|
}, |
|
|
|
|
|
fail: function() { //使用ip获取定位
|
|
|
|
|
|
let key = config.mapKey; //腾讯地图key
|
|
|
|
|
|
getUserAddressByIp(key).then(res => { |
|
|
|
|
|
fn(res.position) //返回经纬度
|
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getLocationDetail() { //获取用户详细地址
|
|
|
|
|
|
wxGetLocation() |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
let key = config.mapKey; //腾讯地图key
|
|
|
|
|
|
uni.getLocation({ |
|
|
|
|
|
type: 'gcj02', |
|
|
|
|
|
isHighAccuracy: true, |
|
|
|
|
|
highAccuracyExpireTime: 1000, |
|
|
|
|
|
success: function(position) { |
|
|
|
|
|
getUserAddress(position.latitude, position.longitude, key).then(res => { |
|
|
|
|
|
resolve(res) |
|
|
|
|
|
}) |
|
|
|
|
|
}, |
|
|
|
|
|
fail: function() { //使用ip获取定位
|
|
|
|
|
|
getUserAddressByIp(key).then(res => { |
|
|
|
|
|
resolve(res) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getUserAddress(latitude, longitude, key) { //通过经纬度获取用户详细地址
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
|
|
|
|
|
|
let url = `/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}` |
|
|
|
|
|
|
|
|
|
|
|
// #ifndef H5
|
|
|
|
|
|
url = `https://apis.map.qq.com` + url |
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
uni.request({ |
|
|
|
|
|
url, |
|
|
|
|
|
success: (res) => { |
|
|
|
|
|
let { |
|
|
|
|
|
lat, |
|
|
|
|
|
lng |
|
|
|
|
|
} = res.data.result.ad_info.location; |
|
|
|
|
|
let data = { |
|
|
|
|
|
position: { |
|
|
|
|
|
latitude: lat, |
|
|
|
|
|
longitude: lng |
|
|
|
|
|
}, |
|
|
|
|
|
addressDetail: res.data.result.ad_info |
|
|
|
|
|
} |
|
|
|
|
|
resolve(data) |
|
|
|
|
|
}, |
|
|
|
|
|
fail(err) { |
|
|
|
|
|
reject(err) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getUserAddressByIp(key) { //根据IP获取当前用户位置
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
uni.request({ |
|
|
|
|
|
url: 'https://api.ipify.org?format=json', |
|
|
|
|
|
success: (ipInfo) => { |
|
|
|
|
|
|
|
|
|
|
|
let url = `/ws/location/v1/ip?ip=${ipInfo.data.ip}&key=${key}` |
|
|
|
|
|
|
|
|
|
|
|
// #ifndef H5
|
|
|
|
|
|
url = `https://apis.map.qq.com` + url |
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
uni.request({ |
|
|
|
|
|
url, |
|
|
|
|
|
success: (res) => { |
|
|
|
|
|
let { |
|
|
|
|
|
lat, |
|
|
|
|
|
lng |
|
|
|
|
|
} = res.data.result.location; |
|
|
|
|
|
let data = { |
|
|
|
|
|
addressDetail: res.data.result.ad_info, |
|
|
|
|
|
ip: res.data.result.ip, |
|
|
|
|
|
position: { |
|
|
|
|
|
latitude: lat, |
|
|
|
|
|
longitude: lng |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
resolve(data) |
|
|
|
|
|
}, |
|
|
|
|
|
fail(err) { |
|
|
|
|
|
reject(err) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//打开地图让用户选择位置
|
|
|
|
|
|
function selectAddress(longitude, latitude, successCallback) { |
|
|
|
|
|
uni.chooseLocation({ |
|
|
|
|
|
// longitude, //经度
|
|
|
|
|
|
// latitude, //纬度
|
|
|
|
|
|
success: function(res) { |
|
|
|
|
|
successCallback && successCallback(res) |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//sdk自带获取位置方法(只支持微信环境),这里只当提示在用了(具体获取地址逻辑上面几个方法已完成)
|
|
|
|
|
|
function wxGetLocation(successCallback, failCallback) { |
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
|
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
console.log('wx.getLocation'); |
|
|
|
|
|
wx.getLocation({ |
|
|
|
|
|
type: 'gcj02', |
|
|
|
|
|
isHighAccuracy: true, |
|
|
|
|
|
highAccuracyExpireTime: 2000, |
|
|
|
|
|
success(res) {}, |
|
|
|
|
|
fail(err) { |
|
|
|
|
|
if(err.errMsg == 'getLocation:gps closed'){ |
|
|
|
|
|
uni.showToast({ |
|
|
|
|
|
title: '请打开GPS定位,否则定位不准确', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default { |
|
|
export default { |
|
|
calculateDistance, //计算两点距离
|
|
|
calculateDistance, //计算两点距离
|
|
|
getLocationDetail, //获取用户详细地址
|
|
|
|
|
|
getLocation, //获取用户经纬度
|
|
|
|
|
|
selectAddress, //打开地图让用户选择位置
|
|
|
|
|
|
wxGetLocation, |
|
|
|
|
|
} |
|
|
} |