首页 文章

cordova-plugin-geolocation无法在Android设备上运行

提问于
浏览
0

我在我的ionic / cordova app中实现了cordova-plugin-geolocation插件 . 当我在浏览器和iOS设备上运行应用程序时,它的工作正常 . 但是,当我尝试在Android设备上运行时, Map 不会显示,也无法获得当前的GPS坐标 . 超时后会抛出以下错误:

[对象PositionError]

首先我添加了插件(以及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']);

我的 Map 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设备..任何帮助?

3 回答

  • 0

    我无法让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);
          });
    
  • 3

    我没有那么多关于如何修复它的信息 . 我遵循了这个教程:http://www.gajotres.net/using-cordova-geoloacation-api-with-google-maps-in-ionic-framework/我认为不仅要安装白名单插件,还要在index.html中使用正确的元标记(您可以在教程中找到元标记) . 完全不在Android设备上工作 .

  • 0

    我遇到了完全相同的错误,解决方案是将以下内容添加到AndroidManifest.xml(位于平台 - > 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" />
    

    显然,插件没有向清单添加权限 .

相关问题