Created
July 2, 2015 15:14
-
-
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
This file contains hidden or 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
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