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?