Skip to content

Instantly share code, notes, and snippets.

@xavierlacot
Created March 22, 2013 09:08
Show Gist options
  • Save xavierlacot/5219919 to your computer and use it in GitHub Desktop.
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.
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();
}
@xavierlacot
Copy link
Author

Example use:

// guess there's a "textField" Ti.UI.TextField in the application
forwardGeoCoder(textField.value, function(latitude, longitude) {
  // create a Ti.Map.View
  var map = Titanium.Map.createView({
    mapType: Titanium.Map.STANDARD_TYPE,
    region: {
      latitude:latitude,
      longitude:longitude,
      latitudeDelta:0.01,
      longitudeDelta:0.01
    },
    animate:true,
    regionFit:false,
    userLocation:false,
    top: 60
  });

  // add the map to the current view
  self.add(map);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment