【问题标题】:Run function after geolocation permission accepted接受地理位置权限后运行功能
【发布时间】:2019-01-16 09:39:55
【问题描述】:

我遇到了一个地理定位问题,我需要在 iOS 上为用户显示geolocation 提示并且他们点击“接受”之后运行一个函数。

目前我正在ngOnInit上检查用户位置,但如果他们还没有接受,我在检查用户位置后运行的功能不起作用。

这是我目前拥有的:

this.geolocation.getCurrentPosition({enableHighAccuracy: false, timeout: 3000, maximumAge: 100000})
.then((resp) => {
   this.userLocation.lat = resp.coords.latitude;
   this.userLocation.lng = resp.coords.longitude;
   this.getLocation();
}).catch((error) => {
   console.log(error)    
});

所以this.getLocation();函数在geolocation找到位置后运行,但是在用户接受“允许此应用程序使用您的位置”提示后我将如何运行此代码?

谢谢!

【问题讨论】:

    标签: javascript cordova ionic-framework ionic2 geolocation


    【解决方案1】:

    cordova-plugin-geolocation 不允许这样做。

    您可以使用其他插件,例如 cordova-diagnostic-plugin,允许您请求任何权限。

    cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
        switch(status){
            case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
                console.log("Permission not requested");
                break;
            case cordova.plugins.diagnostic.permissionStatus.DENIED:
                console.log("Permission denied");
                break;
            case cordova.plugins.diagnostic.permissionStatus.GRANTED:
                console.log("Permission granted always");
                break;
            case cordova.plugins.diagnostic.permissionStatus.GRANTED_WHEN_IN_USE:
                console.log("Permission granted only when in use");
                break;
        }
    }, function(error){
        console.error(error);
    }, cordova.plugins.diagnostic.locationAuthorizationMode.ALWAYS);
    

    Docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-09
      • 1970-01-01
      • 2017-10-17
      • 2023-01-31
      • 2019-10-27
      • 1970-01-01
      • 2017-07-06
      • 2016-07-26
      相关资源
      最近更新 更多