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