2

i have been trying to run this code to get latitude and longitude. My IE browser shows the message that geolocation services are not supported, and Chrome says 'This page has been blocked from tracking your location' even after i set the location setting in chrome to allow all sites to track. Please help.

<html>
<head></head>
<body>
    <input type="button" id="btnSearch" value="Search" onclick="doSomething();"/>
    <script>
    function doSomething(){
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(handle_geolocation_query);
            function handle_geolocation_query(position) {
                alert('Lat: ' + position.coords.latitude + ' ' + 'Lon: ' + position.coords.latitude);
            }
        } else {
            alert("I'm sorry, but geolocation services are not supported by your browser.");
        }
    }
    </script>
</body>
</html>
David Jones
  • 4,275
  • 6
  • 27
  • 51
j4rey
  • 2,582
  • 20
  • 34

3 Answers3

0

Change the scope of handle_geolocation_query() to outer, and it should work. It works on my IE and firefox after the changes.

<html>
<head></head>
<body>
    <input type="button" id="btnSearch" value="Search" onclick="doSomething();"/>
    <script>
    function doSomething(){
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(handle_geolocation_query);
        } else {
            alert("I'm sorry, but geolocation services are not supported by your browser.");
        }
    }

    function handle_geolocation_query(position) {
        alert('Lat: ' + position.coords.latitude + ' ' + 'Lon: ' + position.coords.latitude);
    }
    </script>
</body>
</html>    
Chris Lim
  • 424
  • 4
  • 9
0

In my case the problem was that I opened the HTML file from the file system (file:///...). Browsers generally try to prevent accessing personal information (which includes location) from local files, so you have to serve the file through a web server (even if it is local).

One simple way to serve a static website located in your filesystem is SimpleHTTPServer if you have Python installed. Just navigate to the folder using the command prompt, and say python -m SimpleHTTPServer, and then you can view the file on localhost:8000.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
-1

try removing the if statement that wraps it and see what happens. the navigator.geolocation.getCurrentPosition is the function that calls for the browsers permission to access the geo service, so the problem I think is that it is not reaching that point and is skipping straight to the else statement.

Here is a source that should help you clean up your geo code.

Bradley
  • 490
  • 3
  • 9
  • Your code works perfectly in Chrome for me which tells me its likely a problem from your system locally, check this post for the fix. http://stackoverflow.com/questions/5423938/html-5-geo-location-prompt-in-chrome – Bradley Oct 08 '14 at 13:48