Skip to content

Instantly share code, notes, and snippets.

@wallawe
Created July 2, 2015 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallawe/0a8999bfb50c0ee8bd58 to your computer and use it in GitHub Desktop.
Save wallawe/0a8999bfb50c0ee8bd58 to your computer and use it in GitHub Desktop.
Google places angular directive that splits out the city, state, country, latitude and longitude
angular.module('app').directive('googleplace', [ function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, model) {
var options = {
types: ['(cities)'],
componentRestrictions: {}
};
scope.gPlace = new google.maps.places.Autocomplete(element[0], options);
google.maps.event.addListener(scope.gPlace, 'place_changed', function() {
var geoComponents = scope.gPlace.getPlace();
var latitude = geoComponents.geometry.location.A;
var longitude = geoComponents.geometry.location.F
var addressComponents = geoComponents.address_components;
addressComponents = addressComponents.filter(function(component){
switch (component.types[0]) {
case "locality": // city
return true;
case "administrative_area_level_1": // state
return true;
case "country": // country
return true;
default:
return false;
}
}).map(function(obj) {
return obj.long_name;
});
addressComponents.push(latitude, longitude);
scope.$apply(function() {
scope.details = addressComponents;
model.$setViewValue(element.val());
});
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment