Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html navigator "User denied Geolocation"

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.

like image 448
Sagi Mann Avatar asked Jul 30 '12 16:07

Sagi Mann


People also ask

How do I fix user denied request for geolocation?

Go to Settings -> Connections -> Location and enable if necessary. Check the "Blocked" and "Allowed" lists by tapping them.

How accurate is HTML geolocation?

A desktop browser is likely to use WiFi (accurate to 20m) or IP Geolocation which is only accurate to the city level and can provide false positives. Mobile devices tend to use triangulation techniques such as GPS (accurate to 10m and only works outside), WiFi and GSM / CDMA cell IDs (accurate to 1000m).

What does Navigator geolocation return?

The Navigator. geolocation read-only property returns a Geolocation object that gives Web content access to the location of the device. This allows a Web site or app to offer customized results based on the user's location.


2 Answers

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.

like image 86
marteljn Avatar answered Sep 30 '22 04:09

marteljn


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!

like image 35
Charlie Tupman Avatar answered Sep 30 '22 04:09

Charlie Tupman