uniapp中app是否开启定位,app获取地理位置,app导航

1.app是否开启定位

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);
  			}
  		});
  	}
  }

2. app获取地理位置

// 获取当前经纬度
      getCurlocation() {
        uni.getLocation({
          geocode: true,
          type: 'gcj02',
          success: (res) => {
            this.latitude = res.latitude
            this.longitude = res.longitude
            this.current_address = res.address.province + res.address.city + res.address.district + res.address
              .street + res.address.streetNum + res.address.poiName
          },
          fail: (error) => {
            openGps() // 打开定位
          }
        })
      },

3. app导航

// 点击导航
      toAddress(index) {
        let shop_latitude = Number(this.storeList[index].lat);
        let shop_longtude = Number(this.storeList[index].lng);
        let shop_address = this.storeList[index].addr
        let current_latitude = this.latitude;
        let current_longitude = this.longitude;

        var packageName = 'com.autonavi.minimap';
        var main = plus.android.runtimeMainActivity();
        var packageManager = main.getPackageManager();
        var PackageManager = plus.android.importClass(packageManager)
        var packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        if (packageInfo) {
          var Uri = plus.android.importClass("android.net.Uri");
          var url = "amapuri://route/plan?sourceApplication=maxuslife" +
            "&sid=A&slat=" + current_latitude + "&slon=" + current_longitude + "&sname=" + this
            .current_address +
            "&did=B&dlat=" + shop_latitude + "&dlon=" + shop_longtude + "&dname=" + shop_address +
            "D10&dev=0&t=0";
          var Intent = plus.android.importClass('android.content.Intent');
          var intent = new Intent();
          intent.setAction(Intent.ACTION_VIEW);
          intent.addCategory(Intent.CATEGORY_DEFAULT);
          var uri = Uri.parse(url);
          //将功能Scheme以URI的方式传入data  
          intent.setData(uri);
          intent.setPackage("com.autonavi.minimap");
          var main = plus.android.runtimeMainActivity();
          main.startActivity(intent);
        } else {
          // alert('未安装' + packageName + '')
          uni.showToast({
            // title: `目前导航暂只支持${packageName}`,
            title: '目前导航暂只支持高德地图',
            icon:'none'
          })
        }
      }

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值