0

I have created an ionic angularjs ngCordova mobile app, wherein I have used ngCorodova geolocation plugin in order to get user location. when I am testing this on browser it works fine. but when same I [android-app.apk] I install on mobile app [obviously after checking "unknown sources" option]; I am not able to get the location. I see in app setting, permission is there to access location on mobile. Also, When event is trigerred it shows GPS symbol on top bar but it disappears.

Can anybody help me with this?

Below is the code for location in my controller.js

 .directive('reverseGeocode', function ($cordovaGeolocation, $rootScope) {
    return {
        restrict: 'E',
        template: '<div></div>',
        link: function (scope, element, attrs) {
            var geocoder = new google.maps.Geocoder();

            var posOptions = {timeout: 10000, enableHighAccuracy: true};
              $cordovaGeolocation
                .getCurrentPosition(posOptions)
                .then(function (position) {
                  var lati  = position.coords.latitude;
                  var longi = position.coords.longitude;



                 // console.log(angular.toJson($rootScope.lati) + " -  " );

                    var request = new XMLHttpRequest();

                    var method = 'GET';
                    //var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&sensor=true';
                    var async = true;
            //alert(url);
                    //request.open(method, url, async);
                    //alert(angular.toJson(request.open(method, url, async)));
                      //  var data = JSON.stringify(request.responseText);            
                    //  alert(JSON.stringify(request.responseText));

            var latlng = new google.maps.LatLng(lati, longi);

            geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                    //alert(results[1].address_components[1].long_name);
                    $rootScope.colony = results[1].address_components[1].long_name;
                    //alert(results[1].address_components[1].long_name);
                    //alert(results[1].address_components[1].long_name);
                    //alert(angular.toJson(results[1].address_components[1].long_name));
                        element.text(results[1].formatted_address);
                    } else {
                        element.text('Location not found');
                    }
                } else {
                    element.text('Geocoder failed due to: ' + status);
                }
            });                       

                }, function(err) {
                  // error
                });


                var watchOptions = {
frequency : 1000,
timeout : 3000,
enableHighAccuracy: false // may cause errors if true
  };

  var watch = $cordovaGeolocation.watchPosition(watchOptions);
   watch.then(
   null,
   function(err) {
     // error
   },
    function(position) {
     var lat  = position.coords.latitude
      alert("abc >>" + lat);
     var long = position.coords.longitude
    });


    watch.clearWatch();
  // OR
   $cordovaGeolocation.clearWatch(watch)
   .then(function(result) {
     // success
     }, function (error) {
     // error
    });


        },
         replace: true
    }
})

In html file I am using it as :

<h6>
    User Colony: {{ colony }}
    <reverse-geocode lat={{lati}}  lng={{longi}}></reverse-geocode>
  </h6>  
  <a href="#" ng-click="showStores(colony)" class="button button-block button-positive">
      Browse Store
</a>

which triggeres the directive and find lat and long of user.

When testing on browser, it works perfectly but not on mobile itself.

enter image description here

AngryJS
  • 955
  • 5
  • 23
  • 47

1 Answers1

0

In android it is super complicated to work with the GPS user, remember that often the geolocation we get is from the browser and not the GPS itself, and this varies a lot in the devices. For your help, I recommend installing cordova.plugins.diagnostic

function onDeviceReady() {
      cordova.plugins.diagnostic.isLocationAuthorized(function(enabled){
        //alert("gps es : " + (enabled ? "enabled" : "disabled"));
      }, function(error){
          //alert("error: "+error);
      });
      cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
          if(!enabled){
             alert("gps not actived");
          }else{
            navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy: true,timeout: 5000,maximumAge: 5000});
          }
      }, function(error){
          console.log("The following error occurred: "+error);
      });
  }

Always trying to see if I can get a latitude and longitude and if that is not activated or not you can get, it sends a message to the user. I hope it helps you.

NHTorres
  • 1,528
  • 2
  • 21
  • 39