Created
March 22, 2013 09:08
-
-
Save xavierlacot/5219919 to your computer and use it in GitHub Desktop.
Forward Geocoding in javascript - useful in Titanium which restricts the Ti.API.forwardGeocoder method to the US.
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
var forwardGeoCoder = function(address, callback) { | |
xhr = Titanium.Network.createHTTPClient(); | |
var url = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' + address; | |
xhr.open('GET', url); | |
xhr.onload = function() { | |
var json = JSON.parse(this.responseText); | |
if (json.results.length > 0) { | |
var latitude = json.results[0].geometry.location.lat; | |
var longitude = json.results[0].geometry.location.lng; | |
callback(latitude, longitude); | |
} | |
}; | |
xhr.send(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example use: