14

I got strange behavior when I tried to test my "navigator.geolocation.getCurrentPosition" web page. Here is my testing result and code:

my code:

function detectLocation() 
{
  if (navigator.geolocation) 
  {
    navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
    navigator.geolocation.watchPosition(watchGeocodePosition);
  } 
  else 
  {
    onError();
  }
}

this function was run when "body" onload event was called. I had tried to change the timeout to 10000 and 20000, but I still got same result. I also allowed crome and firefox to get my location.

result:

  1. Using chrome (v 17.0.963.79 m), result always went to onError function when navigator.geolocation.getCurrentPosition was called.
  2. Using Firefox (v 10.0.2), result always went to onError function when navigator.geolocation.getCurrentPosition was called.
  3. Using IE (v 9), result was fantastic, I got my current location.

can anyone help me in this strange situation? I really didn't have any idea to solve this problem and I was in hurry on my project deadline. Thanks before.

EDIT : For this couple days I got some progress, the error code code is 2 with a message "Network location provider at 'https://maps.googleapis.com/maps/api/browserlocation/json?browser=chromium&sensor=true' : Response was malformed". Still unsolved, does anyone know how to solve this?

John Hexis
  • 155
  • 1
  • 1
  • 7
  • It seems unusual to me that your if statement doesn't have any real test. I would expect to see something like if (navigator.geolocation!=null) or some such. – Terry Mar 16 '12 at 04:41
  • @Terry Looks like the "if" statement was no problem, this strange result still there even I changed it to what you suggested. Do you know why only IE can retrieve a correct location? actually I was an Indonesian and my current position was in east java. – John Hexis Mar 16 '12 at 07:08
  • you may find differences depending if you are on LAN or wifi, does this affect your result? – ajayel Mar 16 '12 at 08:57
  • @user920962 this result used DSL line through LAN, can you mention exactly what wifi is? from what I know here in my country wifi hot spot was only used a DSL line then they spread it using wifi router. If it was what you mentioned with wifi, the result will be same but when you looked on foursquare it was run correctly in all browser. Is it any special setting or initialization to use navigator.geolocation? – John Hexis Mar 16 '12 at 13:27

7 Answers7

19

I simulated this problem and found that the success callback functions were only called when the html page was hosted on a web server and not when opened from a filesystem.

To test I opened the file directly from my C: drive and it the callbacks didn't work and then hosted the file on Internet Information Services (IIS) and the callbacks did work.

<html>
<body onload="detectLocation()">
<!-- This html must be hosted on a server for navigator.geolocation callbacks to work -->

<div id="status"></div>

<script type="text/javascript">
function detectLocation()
{
  log("detectLocation() starting");
  if (navigator.geolocation)
  {
    log("navigator.geolocation is supported");
    navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
    navigator.geolocation.watchPosition(watchGeocodePosition);
  }
  else
  {
    log("navigator.geolocation not supported");
  }
}
function geocodePosition(){
    log("geocodePosition() starting");
}

function watchGeocodePosition(){
    log("watchGeocodePosition() starting");
}

function onError(error){
    log("error " + error.code);
}
function log(msg){
    document.getElementById("status").innerHTML = new Date() + " :: " + msg + "<br/>" + document.getElementById("status").innerHTML;
}
</script>
</body>
</html>
ajayel
  • 3,027
  • 2
  • 26
  • 33
  • 4
    I have to add one thing, localhost also didn't work. Only a proper public hosting can work with "navigator.geolocation". Also if your dedicated domain server isn't set properly by your ISP, navigator.geolocation won't work. Looks like this is client side script but it depends a lot on server or domain setting. – John Hexis Apr 01 '12 at 06:33
  • most major browsers now require sites to use https:// otherwise access to getCurrentPosition and watchPosition will be denied – ajayel Nov 11 '16 at 03:41
12

I also got this message:

message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 404.", code: 2

I could solve it by switching on my wifi adapter

lukassteiner
  • 787
  • 1
  • 6
  • 25
  • Confirmed! I was working on a laptop using a cellular modem and got the exact same 404 error. Turning my WiFi adaptor back on resolved the error. Odd. – Todd Apr 10 '14 at 22:24
  • Worked here too. Was doing some development remotely and used my iPhone to connect. When connected via the iPhone, got the 404. Found a local wi-fi hotspot, connected, 404 went away. Went back to the tether, 404 came back. – John Shipp Mar 29 '16 at 15:40
11

I had the same issue. Chrome browser wan not returning a position on 30000 miliseconds timeout. Firefox was not returning a position too. I added the option enableHighAccuracy and set it to false but nothing changed(false is the default option). When i change it to true then geolocation started working!
This is my final code,

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
            function(position) {
                // Get current cordinates.
                positionCords = {"lat": position.coords.latitude, "lng": position.coords.longitude};
            },
            function(error) {
                // On error code..
            },
            {timeout: 30000, enableHighAccuracy: true, maximumAge: 75000}
    );
}  
Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
2

You need to be using https, not http.

The Chrome reference for this is here - https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
0

I know this is old topic but recently I had this error also:

message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 404.", code: 2

The fix is to get api key for google maps and use it in your code

<script src='https://maps.googleapis.com/maps/api/jscallback=initMap &signed_in=true&key=YOUR-API-KEY' async defer></script>

Here you can get API KEY: https://developers.google.com/maps/documentation/javascript/get-api-key#key

Combine
  • 3,894
  • 2
  • 27
  • 30
  • This did not work for me. I keep getting the error message `"Network location provider at 'https://www.googleapis.com/' : Returned error code 404."` on some situations. I guess you were lucky and that it was a coincidence. A few times the error was `400` instead of `404`. – Reuel Ribeiro Sep 12 '17 at 01:03
0

I had same issue and solution was to increase the timeout duration as mobile network are slower than wired network

 {timeout: 30000, enableHighAccuracy: true, maximumAge: 75000}

along with enabling cellular positioning

In the Device Settings turn on "Wifi and Cellular positioning" option.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
-3

This will print the Latitude and Longitude of your Location

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
  <script>
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else { 
            alert("Geolocation is not supported by this browser.");
        }

    }
    function showPosition(position) {
        document.getElementById('idLatitude').value = position.coords.latitude;
        document.getElementById('idLongitude').value = position.coords.longitude;

    }
    </script>

</head>
<body onload="getLocation()">
<form action="HelloWorld" method="post">
    <input id="idLatitude"  type="text" name="strLatitude">
    <input id="idLongitude" type="text" name="strLongitude">

</form>
</body>
</html>

enter image description here

sarath
  • 767
  • 12
  • 19