uniapp | Open iOS and Android to implement GPS positioning permissions

It turns out that there are really people who delete all the entertainment software on their mobile phones, regardless of anything, and study day and night, just to return to their original self with light in their eyes and hope. You must firmly believe that every thought you have about learning is the future asking you for help!

Laziness is a very strange thing. It makes you think that comfort is rest and blessing. In fact, it brings you boredom, burnout, and depression. It deprives you of hope for the future and isolates you from others. Friendship makes you increasingly narrow-minded and doubtful about life!

1. Problem description

How does uniapp detect whether GPS is turned on? How to use positioning if the user does not turn on GPS?

2. Solution

For this kind of problem, our common method is to turn on the GPS of the mobile phone. But usually users don’t use this kind of operation much, so they will spend some time to set up and find out how to turn on GPS. Therefore, we need to help users let the mobile phone interface actively go to the GPS positioning turn-on page. This approach is relatively effective. OK

We usually use mobile phone positioning, and there are two systems, Android and iOS, so the method adopted here is to write a public function to call and distinguish based on the judgment of the mobile phone operating system.

code show as below:

/*
 * 打开[ios/安卓]GPS定位权限
 */
export function openGps() {
    
    
	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, // 不显示取消按钮
				success() {
    
    
					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 if (system.platform === 'ios') {
    
    
		var cllocationManger = plus.ios.import("CLLocationManager");
		var enable = cllocationManger.locationServicesEnabled();
		var status = cllocationManger.authorizationStatus();
		plus.ios.deleteObject(cllocationManger);
		console.log("手机系统的定位没有打开");
		uni.showModal({
    
    
			title: '提示',
			content: '请打开定位服务功能',
			showCancel: false, // 不显示取消按钮
			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);
			}
		});
	}
}

3. Frequently Asked Questions

Q1: How to use GPS positioning permission in the project?

uni.getLocation({
    
    
  type: 'gcj02', // 安卓需指定 type 为 gcj02 
  geocode: true, //设置该参数为true可直接获取经纬度及城市信息
  success: (res) => {
    
    
    console.log('当前位置的经度:' + res.longitude);
    console.log('当前位置的纬度:' + res.latitude);
  },
  fail: (error) => {
    
    
    openGps()	// 打开定位
  }
});

Guess you like

Origin blog.csdn.net/weixin_54558746/article/details/125866054