ngCordova with geolocation not working

I am trying to get to use ngCordova for getting my position and geocoding it to get a text formatted address. However it gives me no results as well as errors, which is why I am confused on this matter.

This is what I trying to do

function HomeCtrl($ionicPlatform, $cordovaGeolocation, myDriveApi) {
    var vm = this;
    vm.myLocation = "";
    var geocoder = new google.maps.Geocoder();

    var geoSettings = {
        frequency: 1000,
        timeout: 30000,
        enableHighAccuracy: true // may cause errors if true
    };
    
    $cordovaGeolocation.getCurrentPosition(getPos, error, geoSettings);
    function getPos(position) {
        var LatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        console.log(LatLng);
        return showLocation(LatLng)
    }

    function error(error) {
        alert('code: ' + error.code + '\n' +
        'message: ' + error.message + '\n');
    }

    function showLocation(LatLng) {
        geocoder.geocode({'latLng': LatLng}, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                vm.myLocation = results[0].formatted_address;
                console.log(results);
            }
        })
    };

I have found that I need to use $ionicPlatform, but I dont see it working with or without. Help me out on this matter

You are using it wrong!

All ngCordova plugins use promises, not callbacks. Check docs http://ngcordova.com/docs/plugins/geolocation/

module.controller('GeoCtrl', function($cordovaGeolocation) {
  var posOptions = {timeout: 10000, enableHighAccuracy: false};
  $cordovaGeolocation
    .getCurrentPosition(posOptions)
    .then(function (position) {
      var lat  = position.coords.latitude
      var long = position.coords.longitude
    }, 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
      var long = position.coords.longitude
  });


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

@soutlink

Right, I fixed it using the promises as it supposed to be, here is my code

function HomeCtrl($ionicPlatform, $cordovaGeolocation, myDriveApi) {
    var vm = this;
        vm.myLocation = "";
        var geocoder = new google.maps.Geocoder();

        var geoSettings = {frequency: 1000, timeout: 30000, enableHighAccuracy: false};

        var geo = $cordovaGeolocation.getCurrentPosition(geoSettings);
        geo.then(function (position) {
                var lat = position.coords.latitude,
                    long = position.coords.longitude,
                    LatLng = new google.maps.LatLng(lat, long);
                console.log(LatLng);
                showLocation(LatLng);
            },
            function error(error) {
                alert('code: ' + error.code + '\n' +
                'message: ' + error.message + '\n');
            }
        );

    function showLocation(LatLng) {
        geocoder.geocode({'latLng': LatLng}, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                vm.myLocation = results[0].formatted_address;
                console.log(results);
            }
        })
    };

}

Now this works in the web browser and I can see my coords being gathered and reverse geocoded. So this works fine, but I get no result on my device.

I am using Nexus 5 with the latest OS. Any reasons for this behavior?

The app is working ok? I mean, maybe its a js problem (like google.maps.Geocoder == undefined or so)

Can you debug using Native USB Debugging on chrome?

@soutlink Yeah the app is working fine, I see no errors on the browser, same for the app.

I have just recently changed to use $cordovaGeolocation to get my position on the map, and the same problem occurs, it is working in the browser but not on the device.

I am not sure if I can use native debugging for chrome, but might be a good idea to use logcat and see if there are any errors thrown at me

Maybe a “Whitelist” problem?

Did you “whitelisted” google apis url?

Cant check your code right now, sorry

@soutlink What is this Whitelist problem? Never heard of it before. I run the logcat, and I cant make out anything critical that can be a problem.

This is quite bothersome!

I had this issue once, what solved for me was using ‘navigator’ instead of $cordovaGeolocation, like so:

navigator.geolocation.getCurrentPosition()

I don’t really know why it works like this for me, but it does… I hope it helps :wink:

@brunodb3 Using navigator means not using ngCordova, and it’s difference kind of implementation.
Is ngCordova bugy or there is something else to it?

If nothing works, I will get to use navigator. Might be a good idea to dumb ngCordova

If i use ngCordova, i doesn’t work either, so i am using navigator. I don’t really know why it doesn’t work though…

i heard crosswalk is “bugging” some cordova plugins, you are using it? if yes, already tried without it?

Yes, i’m using CrossWalk, but even without it, ngCordova doesn’t work…

This is quite poor! I will have to change my implementation to use navigator, I been doing this for the past day and it is not fun.

But I noticed that it does give output after waiting for some time uisng ngCordova, only in one instance. In the my homeCtrl for reverse geocoding, but it doesn’t work on displaying markers on the google map at all.
But that also goes for navigator.geolocation it didn’t show any results in the device.

Is there any stable framework api for this that I can use or some way around it? I am not using crosswalk btw. But I got other plugings and dependencies on which might effect it

Regarding the Nexus not working but the browser does, you need to setup the Chrome USB debugging so that you can see the state of the objects.

By the looks of it you are not waiting for your platform to be ready. The first thing you are doing in your controller is trying to get a handle on your google.maps. This doesn’t work on my Nexus 5 because it takes a little longer to fire up the service.

I initially used the ionic.Platform.ready() function at the bottom of my controller but with the recent view caching improvements I have found that using this works better because you know the page is loaded and ready to use:

var geo = {};
$scope.$on('$ionicView.enter', function(event, data) {
    geo = google.maps.Geocoder();
    // carry on with other processing
})

Since Cordova 4.0 you need the Cordova Whitelist plugin to be able to access remote resources like Goole Maps.

Read more about it here:

hi my friend i have an issue with that , the geolocation ng cordovas plugin works in my browser but not in my device i already install the whitelist plugin and i put the security tag in my index.html but it still doesnt work. i dont know what to do google maps dont work, do i have to downgrade de cordova?
what would be your recomendation. thanks

Sounds like you might’ve come across an issue that I’m currently having :slight_smile: see if any of their suggestions work: Ionic Geolocation plugin working on browser but not iOS and Android Emulators

thanks my friend , ive seen of all that suggeston and nothing… :tired_face:. i have
the same issue that u have even the same error code POSITION_UNAVAILABLE.
did you solve the problem?

2016-08-05 18:37 GMT-05:00 Jourdan Bul-lalayao <
ionicframework@discoursemail.com>:

Hey man; nope I was never able to fix it, however, if it helps, the issue does not happen when viewing your app on an iOS Device (through Ionic View or your app itself). Meaning, Geolocation doesn’t work on the iOS Simulator (for the both of us at least), but you can still test your Geolocation feature on your own iOS device without any issues!

Hopefully though, if it is issue with Ionic or Geolocation themselves not working in the iOS Simulator, it’s fixed in an upcoming release >.<.

did you solve that problem? i have the same issue , it works on my browser but not on my note 5 . in my old android device it works!. thats weird!