javascript - geolocation not working in chrome - Stack Overflow

I have used the following script in mozilla and chrome browser. In mozilla, its asking us whether to sh

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744344463a4569597.html

相关推荐

  • javascript - geolocation not working in chrome - Stack Overflow

    I have used the following script in mozilla and chrome browser. In mozilla, its asking us whether to sh

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信