Check out GeoNames

While looking at Kendo UI earlier this morning, I noticed one of their demos made use of GeoNames. I think I had heard of this site before but I had no idea the amount of cool stuff they had there. GeoNames provides a wealth of free geographical information. For example, countries, cities, states, time zones, etc. Again, all free. But on top of the pure data dumps they have some really cool web services. These services are free, but metered. You have to register and get a username, but once you do, it’s pretty simple to use. What I found truly cool though was some of their more unique services. The neatest was findNearByWikipedia. Basically, if you provide a longitude and latitude, the API will find wikipedia articles of items that are near by that location. You can use this to return information about (possibly) interesting things near by the user. Here’s a simple example.

<html> <head> <script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js”></script> <script> $(document).ready(function() {

//exit early if no geolocation
if(!navigator.geolocation) return;
		
//our generic error handler will just give a basic message
function handleError(e){
	$("#status").append("&lt;p&gt;Sorry, I wasn't able to get your location!&lt;/p&gt;");
}

function gotPosition(geo){
	var latitude = geo.coords.latitude;
	var longitude = geo.coords.longitude;
	var apiUrl = "http://api.geonames.org/findNearbyWikipediaJSON?lat="+latitude+"&lng="+longitude+"&username=cfjedimaster&maxRows=10&callback=?";
	$.getJSON(apiUrl, {}, function(res) {

		if (res.hasOwnProperty("status")) {
			$("#status").html("Sorry, I failed to work because: " + res.status.message);
			return;
		}

		var s = "";
		for (var i = 0; i &lt; res.geonames.length; i++) {
			s+= "&lt;h2&gt;"+res.geonames[i].title+"&lt;/h2&gt;&lt;p&gt;";
			if(res.geonames[i].hasOwnProperty("thumbnailImg")) s += "&lt;img src='"+res.geonames[i].thumbnailImg+"' align='left'&gt;";
			s += res.geonames[i].summary;
			s += "&lt;br clear='left'&gt;&lt;a href='http://"+res.geonames[i].wikipediaUrl+"'&gt;[Read More]&lt;/a&gt;&lt;/p&gt;";
		}
		$("#status").html(s);
	});
}

$("#status").html("&lt;p&gt;Getting your location. Please stand by.&lt;/p&gt;");		
navigator.geolocation.getCurrentPosition(gotPosition,handleError);

}); </script> </head>

<body>

<div id=”status”></div>

</body> </html> </code>

This page fires off a HTML5 geolocation request. Once it has it, it then uses jQuery's getJSON and JSON/P support to hit the API. Then it's a simple matter of rendering the results out to the user. You can demo this yourself below, but be warned, my free account will probably stop working if enough people hit the page.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate looking for his next gig. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments