forked from allucardster/ion-place-autocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathion-place-map.js
135 lines (123 loc) · 4.08 KB
/
ion-place-map.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
placeTools
.directive('ionGooglePlaceMap', ['$timeout', function ($timeout) {
return {
restrict: 'E',
template: [
'<div class="ion-place-tools-map-holder" ng-class="{ \'visible\': visible }">',
'<div class="ion-place-tools-map-wrapper">',
'<div class="ion-place-tools-map"></div>',
'<div class="autocomplete-wrap">',
'<ion-google-place radius="options.radius" placeholder="Your address" location-changed="locationChanged" ng-model="data.address" required name="address"/>',
'</div>',
'<div class="controls">',
'<button type="button" class="button button-stable" ng-click="hideModal()">Cancel</button>',
' ',
'<button type="button" class="button button-positive" ng-click="callSuccess()">OK</button>',
'</div>',
'</div>',
'</div>'
].join(''),
scope: {
visible: '=',
data: '=',
options: '=',
onSuccess: '&'
},
replace: true,
link: function (scope, element, attrs) {
options = angular.extend({
radius: 15000,
fitBounds: true,
marker: true
}, scope.options);
var mapEl = element.find('div')[0].children[0],
mapProp = {
center: new google.maps.LatLng(51.508742,-0.120850),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map(mapEl, mapProp),
geocoder = new google.maps.Geocoder(),
marker = new google.maps.Marker({
position: mapProp.center,
map: map,
draggable: true
});
if (!options.marker) {
marker.setVisible(false);
}
scope.hideModal = function () {
scope.visible = false;
};
scope.callSuccess = function () {
scope.hideModal();
if (scope.onSuccess) {
scope.onSuccess()();
}
};
scope.locationChanged = function (address) {
geocoder.geocode({
address: address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
scope.data.latitude = results[0].geometry.location.lat();
scope.data.longitude = results[0].geometry.location.lng();
map.setCenter(results[0].geometry.location);
marker.setPosition(results[0].geometry.location);
if (options.fitBounds) {
map.fitBounds(results[0].geometry.viewport);
}
}
});
};
var geoCodeByCoords = function (latLng) {
geocoder.geocode({
latLng: latLng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
scope.data.address = results[0].formatted_address;
scope.data.latitude = results[0].geometry.location.lat();
scope.data.longitude = results[0].geometry.location.lng();
scope.$apply();
map.setCenter(results[0].geometry.location);
marker.setPosition(results[0].geometry.location);
if (options.fitBounds) {
map.fitBounds(results[0].geometry.viewport);
}
}
});
};
google.maps.event.addListener(map, 'dragend', function() {
geoCodeByCoords(map.getCenter());
});
var infoWindow = null;
google.maps.event.addListener(map, 'click', function (ev) {
geocoder.geocode({
latLng: ev.latLng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (infoWindow) {
infoWindow.close();
}
infoWindow = new google.maps.InfoWindow({
content: results[0].formatted_address,
position: ev.latLng
});
infoWindow.open(map);
}
});
});
google.maps.event.addListener(marker, 'dragend', function () {
geoCodeByCoords(marker.getPosition());
});
navigator.geolocation.getCurrentPosition(function (position) {
latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
geoCodeByCoords(latLng);
});
scope.$watch('options.marker', function(val) {
options.marker = val;
marker.setVisible(options.marker);
});
}
};
}]);