uniapp GPS定位功能的实现

项目场景:

最近在做APP,产品需求是:需检测你的手机是否打开了GPS定位功能,其次只能在所给的地址范围内进行操作,否则就给提示警告。


实现方案:

下面的写法,亲测有效

let system = uni.getSystemInfoSync(); // 获取系统信息
if (system.platform === 'android') {
    
     // 判断平台
		var context = plus.android.importClass("android.content.Context");
		var locationManager = plus.android.importClass("android.location.LocationManager");
		var main = plus.android.runtimeMainActivity();
		var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
		if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
    
    
				uni.showModal({
    
    
					title: '提示',
					content: '请打开定位服务功能',
					showCancel: false, // 不显示取消按钮
					confirmText: "去打开", // 确认按钮文字 
					confirmColor:'#09c499',//删除字体的颜色
					success:async function () {
    
    
							if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
    
    
								    var Intent = plus.android.importClass('android.content.Intent');
								    var Settings = plus.android.importClass('android.provider.Settings');
								    var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
								    main.startActivity(intent); // 打开系统设置GPS服务页面
								}
							}
						})
			        }else{
    
    
			            ····
					}
			    } else if (system.platform === 'ios') {
    
    
			        console.log("苹果");
			        var cllocationManger = plus.ios.import("CLLocationManager");
			        var enable = cllocationManger.locationServicesEnabled();
			        var status = cllocationManger.authorizationStatus();
			        plus.ios.deleteObject(cllocationManger);
			        if (enable && status != 2) {
    
    
			               ····
			        } else {
    
    
			            uni.showModal({
    
    
			                title: '提示',
			                content: '请打开定位服务功能',
			                showCancel: false, // 不显示取消按钮
			                confirmText: "去打开", // 确认按钮文字 
			                confirmColor:'#09c499',//删除字体的颜色
			                success() {
    
    
			                    var UIApplication = plus.ios.import("UIApplication");
			                    var application2 = UIApplication.sharedApplication();
			                    var NSURL2 = plus.ios.import("NSURL");
			                    var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
			                    application2.openURL(setting2);
			                    plus.ios.deleteObject(setting2);
			                    plus.ios.deleteObject(NSURL2);
			                    plus.ios.deleteObject(application2);
			                }
			            });
			        }
			    }
			}

上面的的方法可以拿去直接用,system是检测你的手机是iOS还是android,然后检测你的手机是否打开了定位功能,如果没有,提示,是否打开定位,点去打开直接跳转到手机的GPS地方。接下来,我们在else的地方进行判断是否超过范围。

// 最近使用的时候发现gcj02后面需要加一个空格,要不然不走下面的逻辑,不知道为啥。写的时候,真机跑一下,看看需不需要加空格。
uni.getLocation({
    
    
	type:'gcj02 ',
	geocode:true,
	success:async function (res) {
    
    
	    // 需要后端提供接口返给我们目的地的经纬度
	    // res.longitude 当前经度
		// res.latitude 当前纬度
		// 传给后端返给距离 parseInt(arrT.data.rows[0].distance)
			  if(arrT.data.code == '0'){
    
    
				 if(parseInt(arrT.data.rows[0].distance) < 2000){
    
    
					  uni.navigateTo({
    
    
						   url:''
					  })
				 }else {
    
    
					uni.showToast({
    
    
						title:'当前定位和该位置偏差超过2000米,不能进行操作!',
						duration:2000,
						icon:'none'
					 })
				 }
		    }
	 }
})

总结

好了,今天就分享到这,希望今天的内容能对你有所帮助,记得多多支持哟!

往期文章:
uniapp 图片预览实现
vuex学习篇
vuex的模块化

猜你喜欢

转载自blog.csdn.net/m0_56144469/article/details/127672629