69

Just starting to get into HTML 5 and an testing out geo location...liking it so far. I am hitting a bit of a speed bump though...when I try to get my geo location, chrome automatically blocks the page from getting my location. This does not happen at other sites such as the site below:

http://html5demos.com/geo

The scripts I'm using:

<script type="text/javascript" JavaScript" SRC="geo.js"></script>   
<script type="text/javascript" JavaScript" SRC="Utility.js"></script> 
<script type="text/javascript" JavaScript" SRC="jquery.js"></script> 
<script type="text/javascript" JavaScript" SRC="modernizr.js"></script>  

function get_location() {

        if (geo_position_js.init()) {
            geo_position_js.getCurrentPosition(show_map, handle_error);
        }

    }
    function show_map(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;

        alert("lat:" + latitude + " long:" + longitude);


    }
    function handle_error(err) {
        alert(err.code);
        if (err.code == 1) {
            // user said no!
        }
    }

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(show_map, handle_error);
    } else {
        error('not supported');
    }

I am testing this out from a local directory on my machine, so there isn't really a "domain" like "http://whatever.com/mytestpage.html". Is this why I am not getting prompted? If so, is it possible to force the browswer to request permission to get the user's geo location and is it possible in my scenario?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
AGoodDisplayName
  • 5,543
  • 7
  • 35
  • 50
  • firefox shows the prompt btw in that page – ipopa Mar 25 '11 at 11:32
  • Related question for mobile: [How to enable geolocation for local files on Mobile Chrome](http://stackoverflow.com/questions/12409943/enable-geolocation-for-local-files-on-mobile-chrome) – RyanKDalton Jul 11 '14 at 22:16

9 Answers9

97

There's some sort of security restriction in place in Chrome for using geolocation from a file:/// URI, though unfortunately it doesn't seem to record any errors to indicate that. It will work from a local web server. If you have python installed try opening a command prompt in the directory where your test files are and issuing the command:

python -m SimpleHTTPServer

It should start up a web server on port 8000 (might be something else, but it'll tell you in the console what port it's listening on), then browse to http://localhost:8000/mytestpage.html

If you don't have python there are equivalent modules in Ruby, or Visual Web Developer Express comes with a built in local web server.

robertc
  • 74,533
  • 18
  • 193
  • 177
  • 9
    `python -m http.server` for python 3.x – Feyyaz Jan 13 '13 at 15:59
  • Had the same issue and dropped the file into my local php server (xampp htdocs directory). I'm assuming if you want to use geolocation within a front-end application in development, you can't just run it from your desktop, it has to be server-side. I'm wondering why google decided to add that restriction to Chrome. – Emanegux Jun 12 '13 at 17:46
  • ```php -S localhost:8080``` *php -S : Run with built-in web server.* – iegik Jun 16 '14 at 11:56
  • ```node app.js``` Run simple Node.js content server on port 9615 http://stackoverflow.com/questions/6084360/node-js-as-a-simple-web-server – iegik Jun 16 '14 at 12:11
  • is there any solution for the file:// URI ?? – vineetv2821993 Jul 07 '14 at 15:12
  • I am facing same issue, I cannot switch to python nor can host the page as the content is dynamic and has to be loaded form local drive only. Any other help? – Arti Jan 16 '15 at 12:26
  • Doesn't work for me even if I run a local server. Location is allowed in my settings. – giannis christofakis Mar 28 '15 at 19:38
  • didn't know you could do this with python. super cool thanks :) – veta Aug 03 '15 at 08:23
30

None of the above helped me.

After a little research I found that as of M50 (April 2016) - Chrome now requires a secure origin (such as HTTPS) for Geolocation.

Deprecated Features on Insecure Origins

The host "localhost" is special b/c its "potentially secure". You may not see errors during development if you are deploying to your development machine.

Ryan
  • 2,061
  • 17
  • 28
  • 2
    **For local dev** use `http://sitename.localhost` Google Chrome allows `getCurrentPosition()` on .localhost, Thanks! – Duncanmoo Apr 17 '18 at 12:59
  • @Duncanmoo I tried using `http://sitename.localhost` but it DOES NOT work! – salouri Oct 18 '21 at 06:13
  • @salouri Note that the above answer and comment were from 2017 and 2018, Chrome has made changes since. Also typing something all caps is like shouting, it is also vague and does not allow anyone to help you further. – Duncanmoo Oct 18 '21 at 07:34
6

As already mentioned in the answer by robertc, Chrome blocks certain functionality, like the geo location with local files. An easier alternative to setting up an own web server would be to just start Chrome with the parameter --allow-file-access-from-files. Then you can use the geo location, provided you didn't turn it off in your settings.

Tobias
  • 2,089
  • 2
  • 26
  • 51
3

The easiest way is to click on the area left to the address bar and change location settings there. It allows to set location options even for file:///

enter image description here

Marina Dunst
  • 859
  • 1
  • 6
  • 19
2

Make sure it's not blocked at your settings

http://www.howtogeek.com/howto/16404/how-to-disable-the-new-geolocation-feature-in-google-chrome/

Bishoy Hanna
  • 4,539
  • 1
  • 29
  • 31
2

if you're hosting behind a server, and still facing issues: try changing localhost to 127.0.0.1 e.g. http://localhost:8080/ to http://127.0.0.1:8080/

The issue I was facing was that I was serving a site using apache tomcat within an eclipse IDE (eclipse luna).

For my sanity check I was using Remy Sharp's demo: https://github.com/remy/html5demos/blob/eae156ca2e35efbc648c381222fac20d821df494/demos/geo.html

and was getting the error after making minor tweaks to the error function despite hosting the code on the server (was only working on firefox and failing on chrome and safari):

"User denied Geolocation"

I made the following change to get more detailed error message:

function error(msg) {
  var s = document.querySelector('#status');
  msg = msg.message ? msg.message : msg; //add this line
  s.innerHTML = typeof msg == 'string' ? msg : "failed";
  s.className = 'fail';

  // console.log(arguments);
}

failing on internet explorer behind virtualbox IE10 on http://10.0.2.2:8080 :

"The current location cannot be determined"

davidtingsu
  • 1,090
  • 1
  • 10
  • 17
  • Yes thank you for this, after ages of messing around trying to get this working (or using safari - yuk!) simply changing the url to 127.0.0.1 worked for me. Legend! – Charlie Tupman Aug 18 '16 at 15:33
1

For an easy workaround, just copy the HTML file to some cloud share, such as Dropbox, and use the shared link in your browser. Easy.

0

I too had this problem when i was trying out Gelocation API. I then started IIS express through visual studio and then accessed the page and It worked without any issue in all browsers.

Dilip Agheda
  • 2,447
  • 1
  • 11
  • 15
0

Check Google Chrome setting and permit location access

Change your default location settings.
On your computer, open Chrome.
At the top right, click More Settings.
Under "Privacy and security," click Site settings.
Click Location.
Turn Ask before accessing on or off.

After I changed those settings, Geolocation worked for me.

Kristian
  • 2,456
  • 8
  • 23
  • 23