Bookmark and Share

Wednesday, January 09, 2013

Google locale api: find news based on your IP location



Some experiments with google apis, this page loads the google search control and finds events near your current location. If the page doesn't work please open this post alone.

Your location is....
Loading...

Here is the code used:
<script src="https://www.google.com/jsapi?key=ABQIAAAAQLd5290Kjbk-VPuCDYAfnBRYfKp0eaDcp4nidsc0rBpfwGQjWBRfZ98_xA1kmbh3biVQCxgc_Nv5_Q" 
type="text/javascript">
</script><br />
<script language="Javascript" type="text/javascript">
    //<![CDATA[
 
var position = "";
var latitude = 0;
var longitude = 0;
var city = "";
 
function loadJson(data)
{
if (data!=null)
{
 
$('#geolocation').append(data["geoplugin_city"]); 
$('#geolocation').append("-");
$('#geolocation').append(data["geoplugin_countryName"]); 
 
position = data["geoplugin_city"] + "," + data["geoplugin_countryName"];
city = data["geoplugin_city"];
OnLoad();
}
}
 
function jqLoaded() 
{
var url = "http://www.geoplugin.net/json.gp?jsoncallback=?";
var urlNearBy = "http://www.geoplugin.net/extras/nearby.gp?radius=50&limit=20&format=json&jsoncallback=?";
$.getJSON(url ,  loadJson       ); 
}
 
google.load("jquery", "1.4.3");  
google.setOnLoadCallback(jqLoaded);
 
    google.load("search", "1");
 
 
    function OnLoad() {
      // Create a search control
      var searchControl = new google.search.SearchControl();
 
      // Add in a full set of searchers
      var localSearch = new google.search.LocalSearch();
      searchControl.addSearcher(localSearch);
      searchControl.addSearcher(new google.search.NewsSearch());
      searchControl.addSearcher(new google.search.BlogSearch());
 
      // Set the Local Search center point
      localSearch.setCenterPoint(position);
 
      // Tell the searcher to draw itself and tell it where to attach
      searchControl.draw(document.getElementById("searchcontrol"));
 
      // Execute an inital search
      searchControl.execute(city);
    }
    //google.setOnLoadCallback(OnLoad);
 
    //]]>
    
</script><br />
Some experiments with google apis, this page loads the google search control 
and finds events near your current location. View the source code of the page 
if you want to see the javascript code used.<br />
<br />
<br />
<div id="geolocation">
Your location is....</div>
<div id="searchcontrol">
Loading...</div>
<br />

No comments:

Post a Comment