Google AJAX Loader and ClientLocation via IP
The AJAX APIs from Google brings some cool new features for Javascript developers.
With the AJAX Libraries API the “load”-method of the “google”-object can now load some common Javascript-frameworks which are hostet on Google-servers. For example jQuery or jQuery UI, Dojo, YUI or SWFObject. After including the main javascript file you can use the google object, when finished, a callback-function will be called.
[sourcecode language="html"]
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function() { alert('Ready!');
});
[/sourcecode]
There is also a new and very important property at the google.loader object, if “ClientLocation” is not null then Google have found the location of the current user by IP-address. ClientLocation have a latitude, longitude and address property. If you wanna see it in action look at the example on bohuco.net/code.
[sourcecode language="javascript"]
if (google.loader.ClientLocation) {
// dynamic loading with callback
google.load(“maps”, “2″, {“callback” : function(){
var map = new google.maps.Map2(document.getElementById(“bhc-map”));
map.setCenter(new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude), 11);
}});
}
[/sourcecode]
As you can see above, really simple to create a map from the current position and now we can show the user some local news from news.google.com …
[sourcecode language="javascript"]
google.load(“search”, “1″, {“callback” : function(){
// only code fragment … see example for full code
var search = new google.search.NewsSearch();
search.execute(google.loader.ClientLocation.address.city);
}});
[/sourcecode]