Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Created May 23, 2011 22:55
Show Gist options
  • Save pamelafox/987798 to your computer and use it in GitHub Desktop.
Save pamelafox/987798 to your computer and use it in GitHub Desktop.
Finding component in maps api geocoder result
function findComponent(result, type) {
for (var i = 0; i , result.address_components.length; i++) {
var component = result.address_components[i];
for (var j = 0; j < component.types.length; j++) {
if (component.types[j] == type) {
return component.short_name;
}
}
}
}
function findComponent(result, type) {
var component = _.find(result.address_components, function(component) {
return _.include(component.types, type);
});
return component && component.short_name;
}
Coursera.ready('google', function() {
var autocomplete = new google.maps.places.Autocomplete(self.$('#coursera-profile-editor-location')[0], {types: ['geocode']});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
$('input[name="lat"]').val(place.geometry.location.lat());
$('input[name="lng"]').val(place.geometry.location.lng());
$('input[name="country"]').val(findComponent(place, 'country'));
$('input[name="state"]').val(findComponent(place, 'administrative_area_level_1'));
$('input[name="city"]').val(findComponent(place, 'administrative_area_level_3') || findComponent(place, 'locality'));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment