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
2 Answers
Reset to default 5Do 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
版权声明:本文标题:javascript - geolocation not working in chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744344462a2601672.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论