Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 466
John Hexis Avatar asked Mar 16 '12 04:03

John Hexis


People also ask

Why is Geolocation not working?

This occurs when the geolocation services fail. For some patrons, this error message may appear if you are using an outdated browser such as Internet Explorer 8. Try installing a new browser (e.g., Firefox) and try again. This occurs when the geolocation services fail.

What is navigator Geolocation getCurrentPosition?

Description. The getCurrentPosition method retrieves the current geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed. The location information is returned in a Position object.

What does Navigator Geolocation return?

The Navigator. geolocation read-only property returns a Geolocation object that gives Web content access to the location of the device. This allows a Web site or app to offer customized results based on the user's location.

Which choice is the mandatory parameter of the getCurrentPosition () method?

Explanation: Coordinates of object is return by getCurrentPosition() method. getCurrentPosition() function accepts three parameters i.e. success, position and error. When data is fetched successfully success callback will be invoked.


4 Answers

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>
like image 58
ajayel Avatar answered Oct 17 '22 14:10

ajayel


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

like image 27
lukassteiner Avatar answered Oct 17 '22 16:10

lukassteiner


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}
    );
}  
like image 11
Georgios Syngouroglou Avatar answered Oct 17 '22 15:10

Georgios Syngouroglou


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

like image 2
Chris Halcrow Avatar answered Oct 17 '22 15:10

Chris Halcrow