21

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.

marteljn
  • 6,446
  • 3
  • 30
  • 43
Sagi Mann
  • 2,967
  • 6
  • 39
  • 72

3 Answers3

25

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.

Community
  • 1
  • 1
marteljn
  • 6,446
  • 3
  • 30
  • 43
  • 1
    A really easy solution to this: if you have Python installed, navigate to the root directory of your project in a terminal/command line/shell and run `python -m SimpleHTTPServer`. (Source: http://stackoverflow.com/questions/530787/simple-http-web-server) – Ken Bellows Aug 04 '12 at 20:46
  • Note: Even after using a server and refreshing the page, the console might be empty. Search for a small bar or window at the top of your browser asking for permission for location services. – givanse May 07 '14 at 01:30
12

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

info symbol illustration

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

Hope this helps.

Stphane
  • 3,368
  • 5
  • 32
  • 47
Sumeet
  • 121
  • 1
  • 6
  • Your answer helped - I was looking at Browser Console which had the message "Geolocation permission has been blocked as the user has dismissed the permission prompt several times. This can be reset in Page Info which can be accessed by clicking the lock icon next to the URL. See https://www.chromestatus.com/features/6443143280984064 for more information." This Chrome Message was useless and not even accurate. I could not even find where "Page Info" was to reset it. – joedotnot Jul 03 '20 at 14:59
  • should be the answer. Worked easy – PlayHardGoPro Aug 27 '23 at 13:31
7

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!

Charlie Tupman
  • 519
  • 6
  • 11