Ridan
Ridan

Reputation: 349

Cordova geolocation plugin getCurrentPosition deprecated

I have an ionic app that is trying to use geolocation exactly as shown in the docs.

var posOptions = {timeout: 10000, enableHighAccuracy: true};

$cordovaGeolocation.getCurrentPosition(posOptions)
  .then(function (position) {
    //  do something
  }, function(err) {
    console.log(err);
    // error
  });

But now it has stopped working and in the console gives me this warning.

getCurrentPosition() and watchPosition() are deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.

This seems like a huge change to the w3c spec I am just surprised there is not a lot of documentation regarding it. Can anyone tell me what I am missing here.

The app is running on phones so it's listening on localhost naturally. It is talking to the server over http not https but I don't see why that would affect getting geo-coordinates

I am testing the app on the browser and as a cordova app on an ios device.

Upvotes: 3

Views: 3007

Answers (2)

MindWire
MindWire

Reputation: 4089

When testing out your app in the chrome browser, just change the apps url (generally 192.xxx.xxx.xxx:3000) to localhost:3000

You will need to make sure your media policy supports it, but you will not get a security error.

Upvotes: 2

BartBiczBoży
BartBiczBoży

Reputation: 2672

I am using HTML5 geolocation directly. But cordova plugin is just angular wrapper over it, as they say in this Stack Overflow answer.

Apparently browsers can not use geolocation from http pages any more. But for ionic this is issue only for livereload. There is some workaround described using http-proxy to have livereload working on https.

Running the app on device without livereload (i.e. 'ionic run android' without '-l' at the end) works fine.

Remember to run getCurrentPosition after deviceready event. For me this one works in one of the controllers:

            document.addEventListener("deviceready", function () {
            console.info('deviceready fired!');
            window.navigator.geolocation.getCurrentPosition(function(position) {
                console.info('Location from Cordova:');
                console.info("Latitude: " + position.coords.latitude + "; Longitude: " + position.coords.longitude);
            });

Upvotes: 2

Related Questions