0
votes

I've implemented the cordova-plugin-geolocation plugin in my ionic/cordova app. It works perfectly when I run the app in my browser as well as on an iOS device. However when I try and run it on an Android device the map doesn't get displayed and it doesn't get your current GPS co-ordinates. After it times out it throws the following error:

[object PositionError]

First I added the plugin (as well as the cordova-plugin-whitelist plugin by running this is the cli:

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

I then added the following to my index.html:

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

I then modified my app.js to include 'uiGmapgoogle-maps':

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

My HTML for the map:

 <!-- 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>

And finally the logic in my controller:

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

But, as I said. It works in the browser and iOS devices but not on Android devices.. Any help?

3

3 Answers

3
votes

I could not let working geoLocation 100% good in all cases, so i use this external service, in case cordovaGeoLocation gives me an error:

  /** 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);
      });
1
votes

I encountered the exact same error and the solution is to add the following to the AndroidManifest.xml (Located inside 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" />

Apparently, the plugin did not add permissions into the manifest.

0
votes

I'm answering my own question because I see that this is an issue that lots of people run into and there's not that much info out there on how to fix it. I followed this tutorial: http://www.gajotres.net/using-cordova-geoloacation-api-with-google-maps-in-ionic-framework/ I think it came down to not only installing the whitelist plugin, but using the correct meta tag in your index.html (you can find the meta tag in the tutorial). Working perfectly not on Android devices.