var that = this
// 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  
	}  
);

最后发现,特么的位置获取奇怪的一批感觉被位置欺骗了
很多人说挂后台会被杀死,你可以试着把应用的权限开启,主要是手机为了省电会把应用关掉,你们可以在设置里面把这个权限开启后台运行
本文转载,原文来源于:https://ask.dcloud.net.cn/question/97949

我的感觉就是:获取的不够精准。然后代码拿去自己试吧

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐