Skip to content

Instantly share code, notes, and snippets.

@adambarthelson
Created June 11, 2014 00:48
Show Gist options
  • Save adambarthelson/0db5bc4ea18e65b74e84 to your computer and use it in GitHub Desktop.
Save adambarthelson/0db5bc4ea18e65b74e84 to your computer and use it in GitHub Desktop.
Custom Angular directive for geocoding and storing location data from search. http://hpneo.github.io/gmaps/examples/geocoding.html
app.directive("gmap", function() {
return {
restrict: "E",
scope: false,
replace: false,
templateUrl: "map.html",
link: function(scope){
map = new GMaps({
div: '#map',
lat: scope.latitude,
lng: scope.longitude
});
map.addMarker({
lat: scope.latitude,
lng: scope.longitude
});
$('#geocoding_form').submit(function(e){
e.preventDefault();
GMaps.geocode({
address: $('#address').val().trim(),
callback: function(results, status){
if(status=='OK'){
var latlng = results[0].geometry.location;
map.setCenter(latlng.lat(), latlng.lng());
map.addMarker({
lat: latlng.lat(),
lng: latlng.lng()
});
scope.$apply(function(){
scope.latitude = latlng.lat();
scope.longitude = latlng.lng();
});
}
}
})
});
}
}
});
... HTML/CSS Stuff...
... ng-app stuff...
<gmap></gmap>
<!--Google Maps-->
<script src='//maps.googleapis.com/maps/api/js?sensor=true'></script>
<script src='bower_components/gmaps/gmaps.js'></script>
<form method="post" id="geocoding_form">
<label for="address">Address:</label>
<div class="input">
<input type="text" id="address" name="address" />
<input type="submit" class="btn" value="Search" />
</div>
</form>
<div id="map"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment