Reputation: 1143
So, i have this website and i need to geolocate the position of the visitor.
I saw this code at google code :
<script type="text/javascript" src="gears_init.js"></script>
<script type="text/javascript">
var geo = google.gears.factory.create('beta.geolocation');
function updatePosition(position) {
alert('Current lat/lon is: ' + position.latitude + ',' + position.longitude);
}
function handleError(positionError) {
alert('Attempt to get location failed: ' + positionError.message);
}
geo.getCurrentPosition(updatePosition, handleError);
</script>
Seems pretty much what i need but i must know if the visitor is near City X or if it's closer to City Y
Any way i can do this?
Upvotes: 1
Views: 1440
Reputation: 612
Once you grab a latitude longitude pair for your visitor, to find which city they are closest to, you may want to implement a search of haversine distance combined with lat long of all cities. You can find lat long info for most cities from geonames: http://download.geonames.org/export/dump/ and more info on haversine distance is here: http://rosettacode.org/wiki/Haversine_formula
Upvotes: 0
Reputation: 2289
After you find the user's position, you need to do a reverse geolocation (see here). Then you can use the "city" field of the returned result to see where google puts this user.
If you would take a suggestion, you can also implement visitor's geolocation through HTML5's navigator.geolocation object, and with this you will also serve visitors that use other browsers that don't have google gears installed.
Upvotes: 2