当前位置:   article > 正文

uniapp APP端 定位未授权 去手机设置里开启定位_uniapp app端 打开定位服务

uniapp app端 打开定位服务

1、在页面使用
import { getLocation } from “@/utils/location.js”;
getLocation()获取地址
2、创建location文件

import {checkOpenGPSServiceByAndroid } from './openSettings.js'
export function getLocation() {
    return new Promise((resolve, reject) => {
        uni.getLocation({
            type: "wgs84 ",
            success(res) {},
            fail: function (err) {
                uni.showModal({
                    title: '提示',
                    content: '需要开启定位权限才能获取当前位置,请前往设置中心开启。',
                    confirmText: '去开启',
                    showCancel:false,
                    success: function(res) {
                      if (res.confirm) {
                        // 用户点击确认,跳转到设置页面
                        checkOpenGPSServiceByAndroid()
                      } else {
                      }
                    }
                  });
            },
            complete(com) { },
        });
    })
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

3、创建openSettings文件

let system = uni.getSystemInfoSync(); // 获取系统信息
/**检查是否打开GPS功能(android)**/
export const checkOpenGPSServiceByAndroid = () => {
  if (system.platform === 'android') { // 判断平台
      openGps()
  }else  if (system.platform == "ios") {
  //苹果打开对应的通知栏
  uni.showModal({
	  title: "定位权限开启提醒",
	   content: "未开启定位,请前往设置!",
	   confirmText: "去设置",
	   success: function(res) {
       if (res.confirm) {
          var app = plus.ios.invoke("UIApplication", "sharedApplication");
          var setting = plus.ios.invoke("NSURL", "URLWithString:", "app-settings:");
          plus.ios.invoke(app, "openURL:", setting);
           plus.ios.deleteObject(setting);
           plus.ios.deleteObject(app);
	    }
      }
  });
  }
 }
    /**
     * 定位权限及弹出权限弹框
     * **/
let num = 0;
export const openGps = () => {
 plus.android.requestPermissions(
     ['android.permission.ACCESS_FINE_LOCATION'],
     function(resultObj) {
       var result = 0;
       for (var i = 0; i < resultObj.granted.length; i++) {
           var grantedPermission = resultObj.granted[i];//'已获取的权限:
           result = 1
       }
       for (var i = 0; i < resultObj.deniedPresent.length; i++) {
         var deniedPresentPermission = resultObj.deniedPresent[i];//拒绝本次申请的权限
          num += 1
          result = 0
       }
       for (var i = 0; i < resultObj.deniedAlways.length; i++) {
          var deniedAlwaysPermission = resultObj.deniedAlways[i];//永久拒绝申请的权限
          num += 1
          result = -1
        }
         // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
        if (result != 1) {
          setTimeout(() => {
           var Intent =plus.android.importClass("android.content.Intent")
           var Settings = plus.android.importClass("android.provider.Settings")
           var Uri = plus.android.importClass("android.net.Uri")
           var mainActivity = plus.android.runtimeMainActivity()
           var intent = new Intent()
           intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
           var uri = Uri.fromParts("package", mainActivity.getPackageName(), null)
           intent.setData(uri)
           mainActivity.startActivity(intent)
          }, 1200)
        }else {
           //因为安卓手机  手机授权之后还需要打开定位服务功能所有当手机授权之后 调用下面的方法
          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: '请打开定位服务功能',
           success(res) {
             if (res.confirm) {
               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 {}
            }
         }
             });
             return
         }
     }
 },
     function(error) {
         resolve({
             code: error.code,
             message: error.message
         });
     }
 );
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/477587
推荐阅读
相关标签
  

闽ICP备14008679号