代码之家  ›  专栏  ›  技术社区  ›  Simon

接受地理位置权限后运行函数

  •  0
  • Simon  · 技术社区  · 6 年前

    我在地理位置上遇到了一个问题,我需要在 geolocation 提示出现在iOS上,提示用户单击“接受”。

    目前我正在做的是 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找到位置后运行,但在用户接受“允许此应用程序使用您的位置”提示后,如何运行此代码?

    谢谢您!

    1 回复  |  直到 6 年前
        1
  •  0
  •   jcesarmobile    6 年前

    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