javascript 地理定位在 safari 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7350033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Geolocation not working in safari
提问by Alex
Im trying to implement the geolocation services to get the latitude and longitude values, but in Safari it just doesnt work. And then in firefox it works, but it doesnt know how to handle errors. I have no idea why.
我试图实现地理定位服务以获取纬度和经度值,但在 Safari 中它不起作用。然后在 Firefox 中它可以工作,但它不知道如何处理错误。我不知道为什么。
Below is my code.
下面是我的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<title>GPS</title>
</head>
<body>
<strong>Output:</strong>
<input type="text" id = "mapLink" value="">
<script type = "text/javascript">
navigator.geolocation.getCurrentPosition(showMap, handle_error);
function showMap(position){
var lat = position.coords.latitude;
var long = position.coords.longitude;
var linkUrl = lat + "," + long;
var mapLink = document.getElementById("mapLink");
mapLink.value = linkUrl;
}
function handle_error(err) {
alert('error');
}
</script>
</body>
</html>
回答by Michael Irwin
Safari does support Geolocation. But to make your life easier, check out http://code.google.com/p/geo-location-javascript/.
Safari 确实支持地理定位。但为了让您的生活更轻松,请查看http://code.google.com/p/geo-location-javascript/。
回答by Gustavo Novaro
You can try, but safari support geo only in new versions, you can try in Google Chrome or Firefox
你可以试试,但是safari只在新版本支持geo,你可以在谷歌浏览器或者火狐浏览器试试
<script>
// Try W3C Geolocation (Preferred)
if(navigator.geolocation) {
alert('Browset support geo')
}
</script>
I implement parctialy geolocation example in this site www.baireswifi.com.aryou can try in your safari. If not working is for use older browser.
我在这个网站www.baireswifi.com.ar 中实现了特定的地理定位示例,您可以在您的 safari 中尝试。如果不工作是为了使用旧浏览器。
回答by Olga
It has to go through HTTPS. The release notes say "WebGeolocation now requires a secure (HTTPS) website to work on both iOS and macOS to prevent malicious use of location data." under the "Safari" heading. https://forums.developer.apple.com/thread/54276
它必须通过HTTPS。发行说明说“WebGeolocation 现在需要一个安全 (HTTPS) 网站才能在 iOS 和 macOS 上运行,以防止恶意使用位置数据。” 在“Safari”标题下。 https://forums.developer.apple.com/thread/54276