html navigator “User denied Geolocation”

≯℡__Kan透↙ 提交于 2019-11-27 14:54:40

问题


I have an issue with window.navigator, I'm getting error code 1, "User denied Geolocation" whenever I run the following code as a local html file:

navigator.geolocation.getCurrentPosition(function(position) {
        console.log(position);
    }, function(positionError) {
        console.log(positionError);
    });

The output is coming from the error function, positionError contains:

code: 1
message: "User denied Geolocation"

This does not happen if the containing html is served from some server.

Is this expected? Is there some way to use navigator from a local html? I am trying to write some mobile app, but am also trying to avoid network whenever possible.

thanks.


回答1:


If you are using chrome, please have a look at the answer below:

HTML 5 Geo Location Prompt in Chrome

It appears this is a security restriction for the file protocol. Looks like you are going to need to host it locally from a server.




回答2:


A simple response that worked for me was to simply change my url from

localhost

to

127.0.0.1

Chrome asked me to confirm I wanted to share my location settings and I was away!




回答3:


Just click on the left side of the url on the little 'i' symbol:- (Refer the image below)

Then check if the location is 'block'. Change it to 'allow' and then reload the page.

Hope this helps.



来源:https://stackoverflow.com/questions/11725594/html-navigator-user-denied-geolocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!