Ionic 2/Ionic 3:如何获取设备的当前位置

kum*_*dan 20 android google-maps-api-3 ionic-framework ionic2 ionic3

不要将此问题标记为重复,stackoverflow上的任何答案都不 适合我.其中很多是针对Ionic 1或者那些答案已被弃用或者它们不适用于Android设备.

我已经在stackoverflow上看到很多关于获取设备当前位置的解决方案,但是没有解决方案似乎适用于Android.

我试过的: -

  • 使用geolocation.getCurrentPosition(),适用于IOS浏览器,但不适用于Android.

  • 使用this.geolocation.watchPosition() ,适用于IOS浏览器,但不适用于Android.

  • 使用navigator.geolocation.getCurrentPosition(),适用于IOS浏览器,但不适用于Android.

  • 使用此问题提供的小提琴解决方案getCurrentPosition()和watchPosition()在不安全的起源上被弃用

无论如何,所有这些都被谷歌弃用,原因是: -

getCurrentPosition()和watchPosition()在不安全的起源上被弃用,将来会删除支持.您应该考虑将应用程序切换到安全的来源,例如HTTPS.有关详细信息,请参阅goo.gl/rStTGz.

这个问题joshmorony(https://www.joshmorony.com/adding-background-geolocation-to-an-ionic-2-application/)解决方案前景不工作为Android物理设备,但做工精细的浏览器和iOS.背景跟踪工作正常,这是第一次花费将近50秒的时间来进行拉动.

请帮我解决一下这个.我想要一种在最短时间内获得当前位置的方法.对于您的信息,我使用谷歌javascript地图sdk/api.

kum*_*dan 5

我尝试了你们所有人和其他人在互联网上提供的所有解决方案。最后我找到了一个解决方案。您可以尝试来自ESRI 的插件cordova-plugin-advanced-geolocationhttps://github.com/Esri/cordova-plugin-advanced-geolocation)。但这个插件适用于Android,不适用于IOS。对于ios,你可以使用相同的旧方法。即 - 使用或。this.geolocation.getCurrentPosition(...)this.geolocation.watchPosition(..)

添加cordova-plugin-advanced-geolocation插件,如下所示:-

cordova plugin add https://github.com/esri/cordova-plugin-advanced-geolocation.git
Run Code Online (Sandbox Code Playgroud)

然后在类/组件顶部添加以下行

declare var AdvancedGeolocation:any;//在类的顶部

现在将这些行添加到组件的相关函数中(PS - 我已经包含了AndroidIOS的代码)

//**For Android**


    if (this.platform.is('android')) {
          this.platform.ready().then(() => {
            AdvancedGeolocation.start((success) => {
              //loading.dismiss();
              // this.refreshCurrentUserLocation();
              try {
                var jsonObject = JSON.parse(success);
                console.log("Provider " + JSON.stringify(jsonObject));
                switch (jsonObject.provider) {
                  case "gps":
                    console.log("setting gps ====<<>>" + jsonObject.latitude);

                    this.currentLat = jsonObject.latitude;
                    this.currentLng = jsonObject.longitude;
                    break;

                  case "network":
                    console.log("setting network ====<<>>" + jsonObject.latitude);

                    this.currentLat = jsonObject.latitude;
                    this.currentLng = jsonObject.longitude;

                    break;

                  case "satellite":
                    //TODO
                    break;

                  case "cell_info":
                    //TODO
                    break;

                  case "cell_location":
                    //TODO
                    break;

                  case "signal_strength":
                    //TODO
                    break;
                }
              }
              catch (exc) {
                console.log("Invalid JSON: " + exc);
              }
            },
              function (error) {
                console.log("ERROR! " + JSON.stringify(error));
              },
              {
                "minTime": 500,         // Min time interval between updates (ms)
                "minDistance": 1,       // Min distance between updates (meters)
                "noWarn": true,         // Native location provider warnings
                "providers": "all",     // Return GPS, NETWORK and CELL locations
                "useCache": true,       // Return GPS and NETWORK cached locations
                "satelliteData": false, // Return of GPS satellite info
                "buffer": false,        // Buffer location data
                "bufferSize": 0,         // Max elements in buffer
                "signalStrength": false // Return cell signal strength data
              });

          });
        } else {

          // **For IOS**

          let options = {
            frequency: 1000,
            enableHighAccuracy: false
          };

          this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {
            // loading.dismiss();
            console.log("current location at login" + JSON.stringify(position));

            // Run update inside of Angular's zone
            this.zone.run(() => {
              this.currentLat = position.coords.latitude;
              this.currentLng = position.coords.longitude;
            });

          });
        }
Run Code Online (Sandbox Code Playgroud)

编辑:第一次安装总是进展顺利。但有时在后续安装中可能会无缘无故地出现错误。要消除此错误(此插件的任何错误)。请按照下列步骤操作:

1.从您的项目中删除此插件(包括config.xmlpackage.json)。

2.删除/移除android平台。

3.删除plugins文件夹。

4.现在,按照上述步骤再次重新安装此插件。