-
Notifications
You must be signed in to change notification settings - Fork 47
/
angulargmMapController.js
469 lines (396 loc) · 13.8 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* Directive controller which is owned by the [gmMap]{@link module:gmMap}
* directive and shared among all other angulargm directives.
*/
(function () {
'use strict';
angular.module('AngularGM').
controller('angulargmMapController',
['$scope', '$element', 'angulargmUtils', 'angulargmDefaults',
'angulargmContainer',
function ($scope, $element, angulargmUtils, angulargmDefaults,
angulargmContainer) {
/** aliases */
var latLngToObj = angulargmUtils.latLngToObj;
var validateLatLng = angulargmUtils.validateLatLng;
var boundsEqual = angulargmUtils.boundsEqual;
var hasNaN = angulargmUtils.hasNaN;
var assertDefined = angulargmUtils.assertDefined;
/*
* 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, angulargmDefaults);
// 'private' properties
this._map = this._createMap(mapId, mapDiv, config, angulargmContainer, $scope);
this._elements = {};
this._objectsNameToScopeIdsMap = {};
this._listeners = {};
// 'public' properties
this.dragging = false;
Object.defineProperties(this, {
'precision': {
value: angulargmDefaults.precision,
writeable: false
},
'center': {
configurable: true, // for testing so we can mock
get: function() {
return latLngToObj(this._map.getCenter());
},
set: function(center) {
if (validateLatLng(center) === null)
throw 'center contains null or NaN';
var changed = 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);
}
}
},
'mapTypeId': {
configurable: true, // for testing so we can mock
get: function() {
return this._map.getMapTypeId();
},
set: function(mapTypeId) {
if (mapTypeId == null)
throw 'mapTypeId was null or unknown';
var changed = this.mapTypeId !== mapTypeId;
if (changed) {
this._map.setMapTypeId(mapTypeId);
}
}
}
});
this._initDragListeners();
$scope.$on('$destroy', angular.bind(this, this._destroy));
};
// Retrieve google.maps.MapOptions
this._getConfig = function($scope, angulargmDefaults) {
// Get config or defaults
var defaults = angulargmDefaults.mapOptions;
var config = {};
angular.extend(config, defaults, $scope.gmMapOptions());
return config;
};
// Create the map and add to angulargmContainer
this._createMap = function(id, element, config, angulargmContainer) {
var map = angulargmContainer.getMap(id);
if (!map) {
map = new google.maps.Map(element[0], config);
angulargmContainer.addMap(id, map);
} else {
var div = map.getDiv();
element.replaceWith(div);
this._map = map;
this.mapTrigger('resize');
map.setOptions(config);
}
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._destroy = function() {
angular.forEach(this._listeners, function(listener) {
angular.forEach(listener, function(l) {
google.maps.event.removeListener(l);
});
});
this._listeners = {};
var self = this;
var types = Object.keys(this._elements);
angular.forEach(types, function(type) {
var scopeIds = Object.keys(self._getElements(type));
angular.forEach(scopeIds, function(scopeId) {
self.forEachElementInScope(type, scopeId, function(element, id) {
self.removeElement(type, scopeId, id);
});
});
});
var streetView = this._map.getStreetView();
if (streetView && streetView.getVisible()) {
streetView.setVisible(false);
}
};
/**
* 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
*/
this.addMapListener = function(event, handler) {
var listener = google.maps.event.addListener(this._map, event, handler);
if (this._listeners[event] === undefined) {
this._listeners[event] = [];
}
this._listeners[event].push(listener);
};
/**
* 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
*/
this.addMapListenerOnce = function(event, handler) {
var listener = google.maps.event.addListenerOnce(this._map, event, handler);
if (this._listeners[event] === undefined) {
this._listeners[event] = [];
}
this._listeners[event].push(listener);
};
/**
* Alias for google.maps.event.addListener(object, event, handler)
*/
this.addListener = function(object, event, handler) {
google.maps.event.addListener(object, event, handler);
};
/**
* Alias for google.maps.event.addListenerOnce(object, event, handler)
*/
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
*/
this.mapTrigger = function(event) {
google.maps.event.trigger(this._map, event);
};
/**
* Alias for google.maps.event.trigger(object, event)
*/
this.trigger = function(object, event) {
google.maps.event.trigger(object, event);
};
this._newElement = function(type, opts) {
if (type === 'marker') {
return new angulargmDefaults.markerConstructor(opts);
} else if (type === 'polyline') {
if (!(opts.path instanceof Array)) {
throw 'polylineOptions did not contain a path';
}
return new angulargmDefaults.polylineConstructor(opts);
} else if (type === 'circle') {
return new angulargmDefaults.circleConstructor(opts);
}
else {
throw 'unrecognized type ' + type;
}
};
this._getElements = function(type) {
if (!(type in this._elements)) {
this._elements[type] = {};
}
return this._elements[type];
};
this._setScopeIdByObjectsName = function(objectsName, scopeId) {
this._objectsNameToScopeIdsMap[objectsName] = scopeId;
};
/**
* Returns the scopeId associated with a gmMarkers/Circles/Polylines expression
* @return {number} scope id
* @throw if objectsName argument is null/undefined
*/
this.getScopeIdByObjectsName = function(objectsName) {
assertDefined(objectsName, 'objectsName');
return this._objectsNameToScopeIdsMap[objectsName];
};
/**
* Adds a new element to the map.
* @return {boolean} true if an element was added, false if there was already
* an element with the given id
* @throw if any arguments are null/undefined or elementOptions does not
* have all the required options (i.e. position)
*/
this.addElement = function(type, scopeId, id, elementOptions, objectsName) {
assertDefined(type, 'type');
assertDefined(scopeId, 'scopeId');
assertDefined(id, 'id');
assertDefined(elementOptions, 'elementOptions');
assertDefined(objectsName, 'objectsName');
if (this.hasElement(type, scopeId, id)) {
return false;
}
if (!this.getScopeIdByObjectsName(objectsName)) {
this._setScopeIdByObjectsName(objectsName, scopeId);
}
var elements = this._getElements(type);
if (elements[scopeId] == null) {
elements[scopeId] = {};
}
// google maps munges passed in options, so copy it first
// extend instead of copy to preserve value objects
var opts = {};
angular.extend(opts, elementOptions);
var element = this._newElement(type, opts);
elements[scopeId][id] = element;
element.setMap(this._map);
return true;
};
/**
* Updates an element on the map with new options.
* @return {boolean} true if an element was updated, false if there was no
* element with the given id to update
* @throw if any arguments are null/undefined or elementOptions does not
* have all the required options (i.e. position)
*/
this.updateElement = function(type, scopeId, id, elementOptions) {
assertDefined(type, 'type');
assertDefined(scopeId, 'scopeId');
assertDefined(id, 'id');
assertDefined(elementOptions, 'elementOptions');
var element = this.getElement(type, scopeId, id);
if (element) {
element.setOptions(elementOptions);
return true;
} else {
return false;
}
};
this.hasElement = function(type, scopeId, id) {
assertDefined(type, 'type');
assertDefined(scopeId, 'scopeId');
assertDefined(id, 'id');
return (this.getElement(type, scopeId, id) != null);
};
/**
* @return {google maps element} the element with the given id, or null if no
* such element exists
*/
this.getElement = function (type, scopeId, id) {
assertDefined(type, 'type');
assertDefined(scopeId, 'scopeId');
assertDefined(id, 'id');
var elements = this._getElements(type);
if (elements[scopeId] != null && id in elements[scopeId]) {
return elements[scopeId][id];
} else {
return null;
}
};
/**
* @return {boolean} true if an element was removed, false if nothing
* happened
*/
this.removeElement = function(type, scopeId, id) {
assertDefined(type, 'type');
assertDefined(scopeId, 'scopeId');
assertDefined(id, 'id');
var elements = this._getElements(type);
var removed = false;
var element = elements[scopeId][id];
if (element) {
element.setMap(null);
removed = true;
}
elements[scopeId][id] = null;
delete elements[scopeId][id];
return removed;
};
/**
* Applies a function to each element on the map.
* @param {String} type of element, e.g. 'marker'
* @param {Function} fn will be called with element as first argument
* @throw if an argument is null or undefined
*/
this.forEachElement = function(type, fn) {
assertDefined(type, 'type');
assertDefined(fn, 'fn');
var elements = this._getElements(type);
var scopeIds = Object.keys(elements);
var allElements = scopeIds.reduce(function(accumulator, scopeId) {
angular.forEach(elements[scopeId], function(element) {
accumulator.push(element);
});
return accumulator;
}, []);
angular.forEach(allElements, function(element, id) {
if (element != null) {
fn(element, id);
}
});
};
/**
* Applies a function to each element in a scope.
* @param {String} type of element, e.g. 'marker'
* @param {number} scope id
* @param {Function} fn will called with marker as first argument
* @throw if an argument is null or undefined
*/
this.forEachElementInScope = function(type, scopeId, fn) {
assertDefined(type, 'type');
assertDefined(scopeId, 'scopeId');
assertDefined(fn, 'fn');
var elements = this._getElements(type);
angular.forEach(elements[scopeId], function(element, id) {
if (element != null) {
fn(element, id);
}
});
};
/**
* Returns the list of google.maps objects by scopeId
* @param {String} type of element, e.g. 'marker'
* @param {number} scope id
* @throw if an argument is null or undefined
*/
this.getElementsByScopeId = function(type, scopeId) {
assertDefined(type, 'type');
assertDefined(scopeId, 'scopeId');
var elements = this._getElements(type);
return elements[scopeId];
};
/**
* Returns the google.maps map instance
*/
this.getMap = function() {
return this._map;
};
/** Instantiate controller */
angular.bind(this, constructor)($scope, $element);
}]);
})();