Where are you? Using the new Ajax ClientLocation API
We just announced two new ways to get location info from a browser client.
The Gears GeoLocation API is very detailed. It is able to use GPS, cell towers, WiFi, and ip addresses to work out the location, and you get an “accuracy” parameter to see what was available. As well as getting a position, you can watch a position so you are updated when a change happens. This is perfect for mobile devices that have Gears installed, and since the community is working on the W3C Geolocation spec it should be in many more places soon.
To go with the Gears API, we also have an API that goes along with the AJAX APIs, called ClientLocation.
This is an ip based geocoder that we have made available, and is very simple.
I put together a trivial example called Where Are You? that ties together this API with the Maps API:
[ http://almaer.com/whereareyou/index.html ]
You get access to the data from google.loader.ClientLocation, which is null if it can’t be calculated.
Here is a bit of JavaScript that ties it together:
google.load("maps", "2.x"); google.setOnLoadCallback(function() { if (google.loader.ClientLocation) { var cl = google.loader.ClientLocation; var location = [cl.address.city, cl.address.region, cl.address.country].join(', '); createMap(cl.latitude, cl.longitude, location); } else { document.getElementById('cantfindyou').innerHTML = "Crap, I don't know. Good hiding!"; } }); function createMap(lat, lng, location) { var mapElement = document.getElementById("map"); mapElement.style.display = 'block'; var map = new google.maps.Map2(mapElement); map.addControl(new GLargeMapControl()); map.addControl(new GMapTypeControl()); map.setCenter(new google.maps.LatLng(lat, lng), 13); map.openInfoWindow(map.getCenter(), document.createTextNode(location)); }




![[image]](http://mowser.com/img?url=http%3A%2F%2Fwww.openhandsetalliance.com%2Fimages%2Fandroid_robot.gif)
![[image]](http://mowser.com/img?url=http%3A%2F%2Fphotos-f.ak.facebook.com%2Fphotos-ak-snc1%2Fv313%2F161%2F33%2F617989202%2Fs617989202_1367261_3940.jpg)