Cordova geolocation plugin getCurrentPosition deprecated

ぐ巨炮叔叔 提交于 2019-12-23 17:44:53

问题


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.


回答1:


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);
            });



回答2:


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.



来源:https://stackoverflow.com/questions/33862925/cordova-geolocation-plugin-getcurrentposition-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!