【问题标题】:cordova-plugin-geolocation not working on Android devicecordova-plugin-geolocation 在 Android 设备上不起作用
【发布时间】:2016-02-02 06:20:35
【问题描述】:

我已经在我的 ionic/cordova 应用程序中实现了 cordova-plugin-geolocation 插件。当我在浏览器和 iOS 设备上运行该应用程序时,它可以完美运行。但是,当我尝试在 Android 设备上运行它时,地图不会显示,也不会获得您当前的 GPS 坐标。超时后会抛出以下错误:

[对象位置错误]

首先我添加了插件(以及cordova-plugin-whitelist插件通过运行这是cli:

cordova plugin add cordova-plugin-geolocation
cordova plugin add cordova-plugin-whitelist

然后我将以下内容添加到我的 index.html:

<script src="//maps.googleapis.com/maps/api/js"></script>
<script src="lib/angular-google-maps/dist/angular-google-maps.min.js"></script>

然后我修改了我的 app.js 以包含“uiGmapgoogle-maps”:

var app = angular.module('myApp', ['ionic', 'ngCordova', 'uiGmapgoogle-maps']);

我的地图 HTML:

 <!-- Google map -->
    <ui-gmap-google-map center='map.center' zoom='map.zoom'>
      <ui-gmap-marker coords="marker.coords" options="marker.options" events="marker.events" idkey="marker.id" closeClick="hideMarkerPopup()" onClicked="showMarkerPopup()"></ui-gmap-marker>
    </ui-gmap-google-map>

最后是我的控制器中的逻辑:

ionic.Platform.ready(function() {

            var posOptions = {
                enableHighAccuracy: true,
                timeout: 20000,
                maximumAge: 0
            };

            // Display 'loading' dialog
            $ionicLoading.show({
                template: 'Loading...'
            });

            $cordovaGeolocation.getCurrentPosition(posOptions).then(function(position) {

                // Apply new values to map
                $scope.location = {};
                $scope.location.lat = position.coords.latitude;
                $scope.location.long = position.coords.longitude;
                $scope.map = {
                    center: {
                        latitude: $scope.location.lat,
                        longitude: $scope.location.long
                    },
                    zoom: 16,
                    pan: 1
                };

                $scope.marker = {
                    id: 0,
                    coords: {
                        latitude: $scope.location.lat,
                        longitude: $scope.location.long
                    }
                };

                $scope.marker.options = {
                    draggable: false
                };

                // Dismiss 'loading' dialog
                $ionicLoading.hide();

            }, function(err) {

                // Dismiss 'please wait' dialog
                $ionicLoading.hide();

                var alertPopup = $ionicPopup.alert({
                    title: 'GPS Error',
                    template: err
                });

            });

        }); // ionic.Platform.ready END

但是,正如我所说。它适用于浏览器和 iOS 设备,但不适用于 Android 设备。有帮助吗?

【问题讨论】:

    标签: angularjs cordova ionic-framework cordova-plugins


    【解决方案1】:

    我不能让 geoLocation 在所有情况下都 100% 好,所以我使用这个外部服务,以防 cordovaGeoLocation 给我一个错误:

      /** i declare de external service */
    
      $scope.getLocation = function (){
        return $http.get("http://ip-api.com/json/" ,{
          headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        }
        }).catch(function(error) {
          console.log(error);
        });
      }
    
     /** and then i use this */
    
    $scope.getLocation().then(function (res){
            var coords = {
              Coordinates : {
                latitude: res.data.lat
                ,longitude: res.data.lon
              }
            };
            $scope.coords = coords.Coordinates;
          }).catch(function (err){
            console.log(err);
          });
    

    【讨论】:

      【解决方案2】:

      我遇到了完全相同的错误,解决方案是将以下内容添加到 AndroidManifest.xml(位于 Platforms -> Android 内部)

       <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
       <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
      

      显然,插件没有在清单中添加权限。

      【讨论】:

      • 这在我在设备上再次运行后为我修复了它,因此它会获取添加的权限。
      【解决方案3】:

      我正在回答我自己的问题,因为我发现这是一个很多人都会遇到的问题,并且没有太多关于如何解决它的信息。我遵循了本教程:http://www.gajotres.net/using-cordova-geoloacation-api-with-google-maps-in-ionic-framework/ 我认为它不仅要安装白名单插件,还要在 index.html 中使用正确的元标记(您可以在教程中找到元标记)。无法在 Android 设备上完美运行。

      【讨论】:

        猜你喜欢
        • 2017-02-03
        • 1970-01-01
        • 2018-04-20
        • 2021-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多