Cordova geolocation插件getCurrentPosition已弃用 [英] 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()和watchPosition()在不安全的来源中已弃用,并且将来会移除支援。您应该考虑将应用程序切换到安全源,例如HTTPS。有关详情,请参见 https://goo.gl/rStTGz 。
这似乎是w3c规格的一个巨大的变化,我只是惊讶,没有很多关于它的文档。任何人都可以告诉我我在这里失踪。
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.
该应用程序正在手机上运行,所以它自然地在localhost上监听。它正在通过http而不是https与服务器通信,但我不明白为什么会影响获取地理坐标
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
我在浏览器上测试应用程序, cordova应用程序在ios设备上。
I am testing the app on the browser and as a cordova app on an ios device.
推荐答案
我直接使用HTML5地理位置。但cordova插件只是它的角包装,因为他们说在这个堆栈溢出回答 。
I am using HTML5 geolocation directly. But cordova plugin is just angular wrapper over it, as they say in this Stack Overflow answer.
显然,浏览器不能再从http网址使用地理位置。但对于离子这是问题只有livereload。 有是使用http-proxy在https上执行livereload的一些解决方法。
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.
在没有livereload的设备上运行应用程序(即离开运行android,末尾没有-l)可以正常运行。
Running the app on device without livereload (i.e. 'ionic run android' without '-l' at the end) works fine.
记住在deviceready事件后运行getCurrentPosition。对我来说,这一个在一个控制器中工作:
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);
});
这篇关于Cordova geolocation插件getCurrentPosition已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!