Last active
September 24, 2015 18:39
-
-
Save alikrc/831682f10e33be0d4160 to your computer and use it in GitHub Desktop.
Simple handy example of google places api with angularjs directive
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
html: | |
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script> | |
<div ng-app="app" ng-controller="mainController"> | |
Selected place: {{selectedPlace}} <br/> | |
<input ng-model="selectedPlace" googleplace/> | |
</div> | |
js: | |
var app = angular.module('app', []).controller('mainController', ['$scope', function ($scope) { }]); | |
app.directive('googleplace', function() { | |
return { | |
require: 'ngModel', | |
link: function(scope, element, attrs, model) { | |
var options = { | |
types: [], | |
componentRestrictions: {country: 'tr'} //set country here | |
}; | |
scope.googlePlace = new google.maps.places.Autocomplete(element[0], options); | |
google.maps.event.addListener(scope.googlePlace, 'place_changed', function() { | |
scope.$apply(function() { | |
model.$setViewValue(element.val()); | |
}); | |
}); | |
} | |
}; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment