Created
September 21, 2013 17:24
-
-
Save rufuspollock/6652449 to your computer and use it in GitHub Desktop.
Example of Javascript Geocode function using Mapquest Nominatim API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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