Domanda

Ok, so I'm using the Google Maps API for my website, and am making use of the 'Set Current Location' function.

It works fine for some users, but for others, it's defaulting to the 'wrong' location.

Example 1 I got a friend to test the site whilst in our student house in Liverpool. When he Allows Current Location, it sets his location as his family home in Stafford, which is wayyyy off.

Example 2 Whilst at a University computer, I tested the website and allowed the Current Location feature. This time, it set the location to the building where the University's servers are located, as opposed to the location of the computer (on the other side of Liverpool!)

I don't know enough about IP geolocation, so can't think of a solution, nor can I find any similar stackoverflow questions. Is there a way to set the location in a more accurate way? Perhaps I could set the location using a postcode?

Any help would be appreciated.

Tristan

EDIT: This is the code I have for setting the location, inside the initialize function

if(navigator.geolocation) {

         navigator.geolocation.getCurrentPosition(currentPositionCallback);

        } else {

        alert('The browser does not support geolocation');

        }

    function currentPositionCallback(position) {

         // Create a new latlng based on the latitude and longitude from the users position
         var user_lat_long = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

         // Add a marker using the user_lat_long position
         var userpos = new google.maps.Marker({
         position: user_lat_long,
          icon: new google.maps.MarkerImage("img/icons/icon_home.png", new google.maps.Size(32, 34)),
         map: map
         });

         // Set the center of the map to the users position and zomm into a more detailed level
         map.setCenter(user_lat_long);
         map.setZoom(15);

    }
È stato utile?

Soluzione

The problem is that geolocation is a bit of a dark art. The API uses a number of techniques to work out where it is.

  • The most reliable is if the machine has GPS. This can narrow you down to the nearest ten metres or so.
  • Next, mobile phones can do some work by reporting what cell masts are in the area and the signal strengths. This can be used to triangulate you to within a hundred metres or so.
  • Another method is to look at the IP address. These can be mapped to a geographic location, but it tends to be very very inaccurate. This technique would only be used if your machine has no other method of geolocation. This might explain the cross-town effect with the University computer.
  • The final method (which I think is at play for your friend) is looking at WiFi networks in the area. You remember Google got in trouble a couple of years ago for slurping data off people's WiFi? Well the reason they were looking at the WiFi in the first place was to get a map of WiFi routers. They know the names and locations of every WiFi router in the country (well, nearly all of them). The browser supplies a list of signals it can see, and Google can work out pretty accurately where you are.

If you're using a standard laptop, the odds are that the only method that can be used is this final one. This works well, provided Google's database is up-to-date.

Unfortunately, it can be inaccurate. People are always setting up new routers, upgrading, changing the WiFi details, moving house and taking routers with them, or even just switching them on or off.

If the signals that are detected around your PC don't match those that Google thinks are in your location, then expect odd results. My guess is that your friend has taken a router with him from his home to his student digs? Google probably recognises that router as being from Stafford, and doesn't recognise any of the others in the area (because all the students have changed since they last did a scan), and so it guesses that it must be in Stafford.

When it works, this technique is frighteningly accurate, but when it doesn't, the results can be comically inaccurate. Short of installing a GPS beacon in your laptop, or persuading everyone in your neighbourhood never to change their routers, there's not much you can do about it one way of the other.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top