Created
March 12, 2016 09:57
-
-
Save vkumarsharma/f019f6116844442c609d to your computer and use it in GitHub Desktop.
Angular directive for Geocode service
This file contains 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
export default function geocode() { | |
return { | |
restrict: 'E', | |
scope: { | |
geocode: '=', | |
}, | |
controller: function($scope, uiGmapGoogleMapApi) { | |
var iLat, iLong; | |
if ($scope.geocode) { | |
iLat = parseFloat($scope.geocode.latitude); | |
iLong = parseFloat($scope.geocode.longitude); | |
} else { | |
iLat = 19.090555; | |
iLong = 72.888684; | |
$scope.geocode = new Object; | |
$scope.geocode.__type = "GeoPoint"; | |
$scope.geocode.latitude = iLat; | |
$scope.geocode.longitude = iLong; | |
} | |
var maps = { center: { latitude: iLat, longitude: iLong }, zoom: 12 }; | |
$scope.map = maps; | |
$scope.options = {scrollwheel: false}; | |
$scope.marker = { | |
id: 0, | |
coords: { | |
latitude: iLat, | |
longitude: iLong | |
}, | |
options: { draggable: true }, | |
events: { | |
dragend: function (marker, eventName, args) { | |
$scope.geocode.latitude = marker.getPosition().lat(); | |
$scope.geocode.longitude = marker.getPosition().lng(); | |
var latlng = {lat: parseFloat($scope.geocode.latitude), lng: parseFloat($scope.geocode.longitude)}; | |
$scope.geocoder.geocode({'location': latlng}, function(results, status) { | |
if (status === google.maps.GeocoderStatus.OK) { | |
if (results[1]) { | |
$scope.$apply(function () { | |
$scope.address = results[1].formatted_address; | |
}); | |
} else { | |
window.alert('No results found'); | |
} | |
} else { | |
window.alert('Geocoder failed due to: ' + status); | |
} | |
}) | |
$scope.marker.options = { | |
draggable: true, | |
labelContent: $scope.address, | |
labelAnchor: "100 0", | |
labelClass: "marker-labels" | |
}; | |
} | |
} | |
}; | |
uiGmapGoogleMapApi.then( function (maps) { | |
$scope.geocoder = new maps.Geocoder(); | |
}) | |
}, | |
template: | |
` | |
<div class="row list-view"> | |
<div class="col-lg-12"> | |
<ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" options="options" pan=true> | |
<ui-gmap-marker coords="marker.coords" options="marker.options" events="marker.events" idkey="marker.id"> | |
</ui-gmap-marker> | |
</ui-gmap-google-map> | |
<div ng-cloak> | |
<ul class="show-map-coords"> | |
<li>Latitude: {{marker.coords.latitude}}</li> | |
<li>Longitutde: {{marker.coords.longitude}}</li> | |
</ul> | |
</div> | |
</div> | |
</div> | |
` | |
}; | |
} | |
geocode.$inject = []; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment