Skip to content

Instantly share code, notes, and snippets.

@rufuspollock
Created September 21, 2013 17:24
Show Gist options
  • Save rufuspollock/6652449 to your computer and use it in GitHub Desktop.
Save rufuspollock/6652449 to your computer and use it in GitHub Desktop.
Example of Javascript Geocode function using Mapquest Nominatim API
// Geocoding using Mapquest Nominatim API
//
// Documentation for the API: http://wiki.openstreetmap.org/wiki/Nominatim
// Here's an example query: http://open.mapquestapi.com/nominatim/v1/search?q=detroit&format=json
// geocode function
//
// :param place: is a place name like "Detroit" or "London"
// :callback: function receiving arguments (error, {lon: ..., lat: ...})
function geocode(place, callback) {
var url = 'http://open.mapquestapi.com/search?format=json&q=' + encodeURIComponent(place);
// requires jquery for ajax
jQuery.getJSON(url, function(data) {
if (data.length > 0) {
callback(null, { lon: parseFloat(data[0].lon), lat: parseFloat(data[0].lat) })
} else {
callback(null, null)
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment