-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglemapgeocoder.controller.js
48 lines (38 loc) · 1.71 KB
/
googlemapgeocoder.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
angular.module("umbraco").controller("googlemapgeocoder.controller",
function ($scope, assetsService) {
// initialise variables
var geocoder;
// use assests service to load Google maps api
assetsService.loadJs("https://maps.googleapis.com/maps/api/js?key=");
// called when Google maps api has loaded
function initialize() {
// create geocoder
geocoder = new google.maps.Geocoder();
}
// runs when geocode button in view is clicked
$scope.codeAddress = function () {
var address = $scope.model.value.address;
var lat, lng;
// use Google api to geocode location
initialize();
geocoder.geocode({ 'address': address }, function (results, status) {
// set location if geocode successful
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
$('#clicker').click();
if (status == google.maps.GeocoderStatus.OK) {
// set something to $scope.model.value here.
$scope.model.value.lat = lat;
$scope.model.value.lng = lng;
console.log($scope.model)
var unsubscribe = $scope.$on("formSubmitting", function (ev, args) {
$scope.model.value = { 'address': address, 'lat':lat, 'lng':lng};
//$scope.model.latitude = lat;
//$scope.model.longitude = lng;
});
} else {
alert('Geocode was not successful');
}
});
}
});