directive.js
3.18 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(function (angular) {
var app = angular.module('batmanDelivers');
app.directive("addressSearch", addressSearch);
addressSearch.$inject = ['$parse', '$rootScope', '$http', '$timeout', '$sce', '$rootScope']
function addressSearch($parse, $rootScope, $http, $timeout, $sce, $rootScope) {
return {
templateUrl: '/src/js/directives/address-search/partial.html',
scope: {
address: '=model'
},
link: function (scope, elm, attrs) {
scope.placeholder = attrs["placeholder"];
scope.selected = {
address: null
};
scope.order = $parse(attrs['order'])(scope);
scope.lbl = $parse(attrs['lbl'])(scope);
var autocompleteService = new google.maps.places.AutocompleteService();
var placesService = new google.maps.places.PlacesService($rootScope.map);
scope.request = function (value) {
if (value && value.length > 3) {
autocompleteService.getPlacePredictions({
input: value
}, function (results) {
scope.addresses = results;
scope.$apply();
});
}
}
scope.$watch('selected.address', function (newvalue) {
if (newvalue && newvalue.place_id) {
placesService.getDetails({
placeId: newvalue.place_id
}, function (result) {
scope.result = result;
scope.address.AddressLine1 = scope.result.address_components[1].long_name;
scope.address.AddressLine2 = scope.result.address_components[2].long_name;
scope.address.City = scope.result.address_components[3].long_name;
scope.address.PostCode = scope.result.address_components[0].long_name;
scope.address.Latitude = scope.result.geometry.location.lat();
scope.address.Longitude = scope.result.geometry.location.lng();
scope.address.Description = newvalue.description;
scope.address.valid = true;
if (!scope.address.$marker) {
scope.address.$marker = new google.maps.Marker({
map: $rootScope.map,
position: scope.result.geometry.location,
icon: '.././includes/images/c1.png'
});
} else {
scope.address.$marker.setPosition({ lat: scope.result.geometry.location.lat(), lng: scope.result.geometry.location.lng() });
}
scope.$apply();
scope.$emit('StopChanged');
});
}
});
}
}
}
})(angular);