1. 用uni自带的getlocation获取位置
var time =setInterval(function() {
if (uni.getStorageSync('token') || 0) {
uni.getLocation({
type: 'gcj02 ',
success: function(res) {
uni.setStorageSync('userLocation', JSON.stringify(res));
that.request({
url: '/driver/order/point_upload',
data: {
location: res.longitude + ',' + res.latitude,
locatetime: new Date().getTime() || '',
speed: res.speed || '',
accuracy: res.accuracy || ''
},
method: 'post'
}).then(res => {
uni.showToast({
title: res.msg,
icon: 'none'
});
});
},
fail: function() {
uni.showToast({
title: '获取位置失败',
icon: 'none'
});
}
});
}
}, 3000);
2.用高德地图的js包的方法获取位置
var time = setInterval(function() {
if (uni.getStorageSync('token') || 0) {
that.Amap.getRegeo({
success: data => {
//console.log(data[0].latitude, data[0].longitude);
that.request({
url: '/driver/order/point_upload',
data: {
location: data[0].longitude + ',' + data[0].latitude,
locatetime: new Date().getTime()
},
method: 'post'
}).then(res => {
uni.showToast({
title: res.msg + 'getRegeo',
icon: 'none'
});
});
}
});
}
}, 3000);
3.用plus位置读取的方法获取
var time = setInterval(function() {
if (uni.getStorageSync('token') || 0) {
plus.geolocation.getCurrentPosition(
function(position) {
that.request({
url: '/driver/order/point_upload',
data: {
location: position.coords.longitude + ',' + position.coords.latitude,
locatetime: new Date().getTime(),
speed: position.coords.speed || '',
accuracy: position.coords.accuracy || '',
height: position.coords.altitude || ''
},
method: 'post'
}).then(res => {
uni.showToast({
title: res.msg + 'getCurrentPosition',
icon: 'none'
});
});
},
function(e) {
uni.showToast({
title: e.message,
icon: 'none'
});
},
{ geocode: false, coordsType: 'amap', timeout: 2900 }
);
}
}, 3000);
4.还是用plus的位置监听的方法
plus.geolocation.watchPosition(
function(position) {
if (uni.getStorageSync('token') || 0) {
that.request({
url: '/driver/order/point_upload',
data: {
location: position.coords.longitude + ',' + position.coords.latitude,
locatetime: new Date().getTime(),
speed: position.coords.speed || '',
accuracy: position.coords.accuracy || '',
height: position.coords.altitude || ''
},
method: 'post'
}).then(res => {
uni.showToast({
title: res.msg + 'watchPosition',
icon: 'none'
});
});
}
},
function(e) {
uni.showToast({
title: e.message,
icon: 'none'
});
},
{
provider: 'amap',
//是否使用高精度设备,如GPS。默认是true
enableHighAccuracy: true,
//超时时间,单位毫秒,默认为0
//使用设置时间内的缓存数据,单位毫秒
//默认为0,即始终请求新数据
//如设为Infinity,则始终使用缓存数据
maximumAge: 3000
}
);
最后发现,特么的位置获取奇怪的一批感觉被位置欺骗了
很多人说挂后台会被杀死,你可以试着把应用的权限开启,主要是手机为了省电会把应用关掉,你们可以在设置里面把这个权限开启后台运行
1***@qq.com (作者)
很多人问我咋解决的,我最后是用插件市场的,原生安卓定位插件解决的。但是还是存在安卓高版本杀死问题!
2020-12-02 09:22