John Hexis
John Hexis

Reputation: 155

navigator.geolocation.getCurrentPosition always fail in chrome and firefox

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?

Upvotes: 14

Views: 52302

Answers (7)

Chris Halcrow
Chris Halcrow

Reputation: 31940

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

Upvotes: 2

Hitesh Sahu
Hitesh Sahu

Reputation: 45052

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.

Upvotes: 0

sarath
sarath

Reputation: 893

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

Upvotes: -3

Combine
Combine

Reputation: 4214

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

Upvotes: 0

Georgios Syngouroglou
Georgios Syngouroglou

Reputation: 19944

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}
    );
}  

Upvotes: 12

lukassteiner
lukassteiner

Reputation: 847

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

Upvotes: 12

ajayel
ajayel

Reputation: 3085

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>

Upvotes: 19

Related Questions