最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - geolocation not working in chrome - Stack Overflow

programmeradmin6浏览0评论

I have used the following script in mozilla and chrome browser. In mozilla, its asking us whether to share location. But in chrome its not displaying anything.

<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else {
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>

I have used the following script in mozilla and chrome browser. In mozilla, its asking us whether to share location. But in chrome its not displaying anything.

<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else {
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
Share Improve this question asked May 9, 2016 at 7:28 Syed NasarSyed Nasar 531 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Do you use Chrome50 ? They removed GeoLocation support for non https Sites.

https://developers.google./web/updates/2016/04/geolocation-on-secure-contexts-only

couple of things I have found can cause this problem.

The first is if the web page is being browsed to on a file system, rather than via a web server (eg, you're just opening a html page stored on your desktop via a browser directly, rather than having Apache/IIS serve it via http://localhost/*)

Sometimes however, just hosting it via localhost isn't enough and it still fails. It seems that you often need a publicly hosted site for the script to work. Why this is, I don't know, but I can only assume the client side script actually depends on quite a bit of server/domain settings.

Finally, sometimes be solved by enabling high accuracy (see here: https://developer.mozilla/en-US/docs/Web/API/Geolocation/getCurrentPosition), but the reason this sometimes does work, I am unsure.

发布评论

评论列表(0)

  1. 暂无评论