admin管理员组

文章数量:1402217

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.

本文标签: javascriptgeolocation not working in chromeStack Overflow