/
angulargmMapController.js
388 lines (323 loc) · 10.6 KB
/
angulargmMapController.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
'use strict';
/**
* Directive controller which is owned by the [gmMap]{@link module:gmMap}
* directive and shared among all other angulargm directives.
*/
(function () {
angular.module('AngularGM').
controller('angulargmMapController',
['$scope', '$element', 'angulargmUtils', 'angulargmDefaults',
'angulargmContainer',
function ($scope, $element, angulargmUtils, angulargmDefaults,
angulargmContainer) {
/** aliases */
var latLngEqual = angulargmUtils.latLngEqual;
var boundsEqual = angulargmUtils.boundsEqual;
var hasNaN = angulargmUtils.hasNaN;
var gMDefaults = angulargmDefaults;
var gMContainer = angulargmContainer;
/** constants */
var consts = {};
consts.precision = 3;
/*
* Construct a new controller for the gmMap directive.
* @param {angular.Scope} $scope
* @param {angular.element} $element
* @constructor
*/
var constructor = function($scope, $element) {
var mapId = $scope.gmMapId();
if (!mapId) { throw 'angulargm must have non-empty gmMapId attribute'; }
var mapDiv = angular.element($element[0].firstChild);
mapDiv.attr('id', mapId);
var config = this._getConfig($scope, gMDefaults);
// 'private' properties
this._map = this._createMap(mapId, mapDiv, config, gMContainer, $scope);
this._markers = {};
// 'public' properties
this.dragging = false;
Object.defineProperties(this, {
'precision': {
value: consts.precision,
writeable: false
},
'center': {
configurable: true, // for testing so we can mock
get: function() {
return this._map.getCenter();
},
set: function(center) {
if (hasNaN(center))
throw 'center contains null or NaN';
var changed = !latLngEqual(this.center, center);
if (changed) {
this._map.panTo(center);
}
}
},
'zoom': {
configurable: true, // for testing so we can mock
get: function() {
return this._map.getZoom();
},
set: function(zoom) {
if (!(zoom != null && !isNaN(zoom)))
throw 'zoom was null or NaN';
var changed = this.zoom !== zoom;
if (changed) {
this._map.setZoom(zoom);
}
}
},
'bounds': {
configurable: true, // for testing so we can mock
get: function() {
return this._map.getBounds();
},
set: function(bounds) {
var numbers = !hasNaN(bounds.getSouthWest()) &&
!hasNaN(bounds.getNorthEast());
if (!numbers)
throw 'bounds contains null or NaN';
var changed = !(boundsEqual(this.bounds, bounds));
if (changed) {
this._map.fitBounds(bounds);
}
}
}
});
this._initDragListeners();
$scope.$on('$destroy', angular.bind(this, this._destroy));
};
// Retrieve google.maps.MapOptions
this._getConfig = function($scope, gMDefaults) {
// Get config or defaults
var defaults = gMDefaults.mapOptions;
var config = {};
angular.extend(config, defaults, $scope.gmMapOptions());
return config;
};
// Create the map and add to angulargmContainer
this._createMap = function(id, element, config, gMContainer) {
var map = gMContainer.getMap(id);
if (!map) {
map = new google.maps.Map(element[0], config);
gMContainer.addMap(id, map);
} else {
var div = map.getDiv();
element.replaceWith(div);
}
return map;
};
// Set up listeners to update this.dragging
this._initDragListeners = function() {
var self = this;
this.addMapListener('dragstart', function () {
self.dragging = true;
});
this.addMapListener('idle', function () {
self.dragging = false;
});
this.addMapListener('drag', function() {
self.dragging = true;
});
};
this._destroy = function() {
google.maps.event.clearInstanceListeners(this._map);
var scopeIds = Object.keys(this._markers);
var self = this;
angular.forEach(scopeIds, function(scopeId) {
self.forEachMarkerInScope(scopeId, function(marker, hash) {
self.removeMarkerByHash(scopeId, hash);
});
});
};
/**
* Alias for google.maps.event.addListener(map, event, handler)
* @param {string} event an event defined on google.maps.Map
* @param {Function} a handler for the event
* @ignore
*/
this.addMapListener = function(event, handler) {
google.maps.event.addListener(this._map,
event, handler);
};
/**
* Alias for google.maps.event.addListenerOnce(map, event, handler)
* @param {string} event an event defined on google.maps.Map
* @param {Function} a handler for the event
* @ignore
*/
this.addMapListenerOnce = function(event, handler) {
google.maps.event.addListenerOnce(this._map,
event, handler);
};
/**
* Alias for google.maps.event.addListener(object, event, handler)
* @ignore
*/
this.addListener = function(object, event, handler) {
google.maps.event.addListener(object, event, handler);
};
/**
* Alias for google.maps.event.addListenerOnce(object, event, handler)
* @ignore
*/
this.addListenerOnce = function(object, event, handler) {
google.maps.event.addListenerOnce(object, event, handler);
};
/**
* Alias for google.maps.event.trigger(map, event)
* @param {string} event an event defined on google.maps.Map
* @ignore
*/
this.mapTrigger = function(event) {
google.maps.event.trigger(this._map, event);
};
/**
* Alias for google.maps.event.trigger(object, event)
* @ignore
*/
this.trigger = function(object, event) {
google.maps.event.trigger(object, event);
};
/**
* Adds a new marker to the map.
* @param {number} scope id
* @param {google.maps.MarkerOptions} markerOptions
* @return {boolean} true if a marker was added, false if there was already
* a marker at this position. 'at this position' means delta_lat and
* delta_lng are < 0.0005
* @throw if markerOptions does not have all required options (i.e. position)
* @ignore
*/
this.addMarker = function(scopeId, markerOptions) {
var opts = {};
angular.extend(opts, markerOptions);
if (!(opts.position instanceof google.maps.LatLng)) {
throw 'markerOptions did not contain a position';
}
var marker = new google.maps.Marker(opts);
var position = marker.getPosition();
if (this.hasMarker(scopeId, position.lat(), position.lng())) {
return false;
}
var hash = position.toUrlValue(this.precision);
if (this._markers[scopeId] == null) {
this._markers[scopeId] = {};
}
this._markers[scopeId][hash] = marker;
marker.setMap(this._map);
return true;
};
/**
* @param {number} scope id
* @param {number} lat
* @param {number} lng
* @return {boolean} true if there is a marker with the given lat and lng
* @ignore
*/
this.hasMarker = function(scopeId, lat, lng) {
return (this.getMarker(scopeId, lat, lng) instanceof google.maps.Marker);
};
/**
* @param {number} scope id
* @param {number} lat
* @param {number} lng
* @return {google.maps.Marker} the marker at given lat and lng, or null if
* no such marker exists
* @ignore
*/
this.getMarker = function (scopeId, lat, lng) {
if (lat == null || lng == null)
throw 'lat or lng was null';
var latLng = new google.maps.LatLng(lat, lng);
var hash = latLng.toUrlValue(this.precision);
if (this._markers[scopeId] != null && hash in this._markers[scopeId]) {
return this._markers[scopeId][hash];
} else {
return null;
}
};
/**
* @param {number} scope id
* @param {number} lat
* @param {number} lng
* @return {boolean} true if a marker was removed, false if nothing
* happened
* @ignore
*/
this.removeMarker = function(scopeId, lat, lng) {
if (lat == null || lng == null)
throw 'lat or lng was null';
var latLng = new google.maps.LatLng(lat, lng);
var hash = latLng.toUrlValue(this.precision);
return this.removeMarkerByHash(scopeId, hash);
};
/**
*
* @param {number} scope id
* @param {string} hash
* @returns {boolean} true if a marker was removed, false if nothing
* happened
* @ignore
*/
this.removeMarkerByHash = function(scopeId, hash) {
var removed = false;
var marker = this._markers[scopeId][hash];
if (marker) {
marker.setMap(null);
removed = true;
}
this._markers[scopeId][hash] = null;
delete this._markers[scopeId][hash];
return removed;
};
/**
* Applies a function to each marker.
* @param {Function} fn will called with marker as first argument
* @throw if fn is null or undefined
* @ignore
*/
this.forEachMarker = function(fn) {
if (fn == null) { throw 'fn was null or undefined'; }
var that = this;
var allMarkers = Object.keys(this._markers).reduce(function(markers, key){
angular.forEach(that._markers[key], function(marker){
markers.push(marker);
});
return markers;
}, []);
angular.forEach(allMarkers, function(marker, hash) {
if (marker != null) {
fn(marker, hash);
}
});
};
/**
* Applies a function to each marker.
* @param {number} scope id
* @param {Function} fn will called with marker as first argument
* @throw if fn is null or undefined
* @ignore
*/
this.forEachMarkerInScope = function(scopeId, fn) {
if (fn == null) { throw 'fn was null or undefined'; }
angular.forEach(this._markers[scopeId], function(marker, hash) {
if (marker != null) {
fn(marker, hash);
}
});
};
/**
* Get current map.
* @returns {object}
* @ignore
*/
this.getMap = function() {
return this._map;
};
/** Instantiate controller */
angular.bind(this, constructor)($scope, $element);
}]);
})();