ionic framework - cordova.getCurrentLocation() method not working properly -
i'm, building android ionic app using cordova.geolocation.getcurrentposition
gps break every time, unable current location moves error function.
it's working fine in lower version of android (5.0.0) breaks in higher version (6.0.1 , above).
here code:
var options = { timeout: 10000, maximumage: 100, enablehighaccuracy: false }; $cordovageolocation.getcurrentposition(options).then(function(position) { $scope.usercurrentlat = position.coords.latitude; $scope.usercurrentlng = position.coords.longitude; var source = new google.maps.latlng($scope.usercurrentlat, $scope.usercurrentlng); var destination = new google.maps.latlng(data.data.geolocation.coordinates[0], data.data.geolocation.coordinates[1]); var directionsservice = new google.maps.directionsservice(); $scope.userrestdistance = parsefloat((google.maps.geometry.spherical.computedistancebetween(source, destination)/1000).tofixed(1))+" km"; $ionicloading.hide(); }, function(error) { $ionicloading.hide(); if (error.permission_denied || error.position_unavailable) { $ionicloading.hide(); $location.path("/nogps"); } });
android 6+ requires run-time permission use location. making single request current location, failing due permission being denied access location.
one option use cordova-diagnostic-plugin manage location authorization yourself:
function runmycode(){ var options = { timeout: 10000, maximumage: 100, enablehighaccuracy: false }; $cordovageolocation.getcurrentposition(options).then(function(position) { $scope.usercurrentlat = position.coords.latitude; $scope.usercurrentlng = position.coords.longitude; var source = new google.maps.latlng($scope.usercurrentlat, $scope.usercurrentlng); var destination = new google.maps.latlng(data.data.geolocation.coordinates[0], data.data.geolocation.coordinates[1]); var directionsservice = new google.maps.directionsservice(); $scope.userrestdistance = parsefloat((google.maps.geometry.spherical.computedistancebetween(source, destination)/1000).tofixed(1))+" km"; $ionicloading.hide(); }, function(error) { $ionicloading.hide(); if (error.permission_denied || error.position_unavailable) { $ionicloading.hide(); $location.path("/nogps"); } }); } function handlepermissionresponse(status){ switch(status){ case cordova.plugins.diagnostic.permissionstatus.granted: runmycode(); break; case cordova.plugins.diagnostic.permissionstatus.not_requested: requestlocationpermssion(); break; case cordova.plugins.diagnostic.permissionstatus.denied: requestlocationpermssion(); break; case cordova.plugins.diagnostic.permissionstatus.denied_always: console.error("permission permanently denied - notify user"); break; } } requestlocationpermssion(){ cordova.plugins.diagnostic.requestlocationauthorization(handlepermissionresponse, function(error){ console.error(error); }); } function checklocationpermission(){ cordova.plugins.diagnostic.getlocationauthorizationstatus(handlepermissionresponse, function(error){ console.error(error); }); } // check/request permission first checklocationpermission();
note: on android 5 , below, permission return granted.
Comments
Post a Comment