This repository has been archived by the owner on May 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 517
/
ng-map.js
3546 lines (3161 loc) · 108 KB
/
ng-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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(root, factory) {
if (typeof exports === "object") {
module.exports = factory(require('angular'));
} else if (typeof define === "function" && define.amd) {
define(['angular'], factory);
} else{
factory(root.angular);
}
}(this, function(angular) {
/**
* AngularJS Google Maps Ver. 1.18.4
*
* The MIT License (MIT)
*
* Copyright (c) 2014, 2015, 1016 Allen Kim
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
angular.module('ngMap', []);
/**
* @ngdoc controller
* @name MapController
*/
(function() {
'use strict';
var Attr2MapOptions;
var __MapController = function(
$scope, $element, $attrs, $parse, $interpolate, _Attr2MapOptions_, NgMap, NgMapPool, escapeRegExp
) {
Attr2MapOptions = _Attr2MapOptions_;
var vm = this;
var exprStartSymbol = $interpolate.startSymbol();
var exprEndSymbol = $interpolate.endSymbol();
vm.mapOptions; /** @memberof __MapController */
vm.mapEvents; /** @memberof __MapController */
vm.eventListeners; /** @memberof __MapController */
/**
* Add an object to the collection of group
* @memberof __MapController
* @function addObject
* @param groupName the name of collection that object belongs to
* @param obj an object to add into a collection, i.e. marker, shape
*/
vm.addObject = function(groupName, obj) {
if (vm.map) {
vm.map[groupName] = vm.map[groupName] || {};
var len = Object.keys(vm.map[groupName]).length;
vm.map[groupName][obj.id || len] = obj;
if (vm.map instanceof google.maps.Map) {
//infoWindow.setMap works like infoWindow.open
if (groupName != "infoWindows" && obj.setMap) {
obj.setMap && obj.setMap(vm.map);
}
if (obj.centered && obj.position) {
vm.map.setCenter(obj.position);
}
(groupName == 'markers') && vm.objectChanged('markers');
(groupName == 'customMarkers') && vm.objectChanged('customMarkers');
}
}
};
/**
* Delete an object from the collection and remove from map
* @memberof __MapController
* @function deleteObject
* @param {Array} objs the collection of objects. i.e., map.markers
* @param {Object} obj the object to be removed. i.e., marker
*/
vm.deleteObject = function(groupName, obj) {
/* delete from group */
if (obj.map) {
var objs = obj.map[groupName];
for (var name in objs) {
if (objs[name] === obj) {
void 0;
google.maps.event.clearInstanceListeners(obj);
delete objs[name];
}
}
/* delete from map */
obj.map && obj.setMap && obj.setMap(null);
(groupName == 'markers') && vm.objectChanged('markers');
(groupName == 'customMarkers') && vm.objectChanged('customMarkers');
}
};
/**
* @memberof __MapController
* @function observeAttrSetObj
* @param {Hash} orgAttrs attributes before its initialization
* @param {Hash} attrs attributes after its initialization
* @param {Object} obj map object that an action is to be done
* @description watch changes of attribute values and
* do appropriate action based on attribute name
*/
vm.observeAttrSetObj = function(orgAttrs, attrs, obj) {
if (attrs.noWatcher) {
return false;
}
var attrsToObserve = Attr2MapOptions.getAttrsToObserve(orgAttrs);
for (var i=0; i<attrsToObserve.length; i++) {
var attrName = attrsToObserve[i];
attrs.$observe(attrName, NgMap.observeAndSet(attrName, obj));
}
};
/**
* @memberof __MapController
* @function zoomToIncludeMarkers
*/
vm.zoomToIncludeMarkers = function() {
// Only fit to bounds if we have any markers
// object.keys is supported in all major browsers (IE9+)
if ((vm.map.markers != null && Object.keys(vm.map.markers).length > 0) || (vm.map.customMarkers != null && Object.keys(vm.map.customMarkers).length > 0)) {
var bounds = new google.maps.LatLngBounds();
for (var k1 in vm.map.markers) {
bounds.extend(vm.map.markers[k1].getPosition());
}
for (var k2 in vm.map.customMarkers) {
bounds.extend(vm.map.customMarkers[k2].getPosition());
}
if (vm.mapOptions.maximumZoom) {
vm.enableMaximumZoomCheck = true; //enable zoom check after resizing for markers
}
vm.map.fitBounds(bounds);
}
};
/**
* @memberof __MapController
* @function objectChanged
* @param {String} group name of group e.g., markers
*/
vm.objectChanged = function(group) {
if ( vm.map &&
(group == 'markers' || group == 'customMarkers') &&
vm.map.zoomToIncludeMarkers == 'auto'
) {
vm.zoomToIncludeMarkers();
}
};
/**
* @memberof __MapController
* @function initializeMap
* @description
* . initialize Google map on <div> tag
* . set map options, events, and observers
* . reset zoom to include all (custom)markers
*/
vm.initializeMap = function() {
var mapOptions = vm.mapOptions,
mapEvents = vm.mapEvents;
var lazyInitMap = vm.map; //prepared for lazy init
vm.map = NgMapPool.getMapInstance($element[0]);
NgMap.setStyle($element[0]);
// set objects for lazyInit
if (lazyInitMap) {
/**
* rebuild mapOptions for lazyInit
* because attributes values might have been changed
*/
var filtered = Attr2MapOptions.filter($attrs);
var options = Attr2MapOptions.getOptions(filtered);
var controlOptions = Attr2MapOptions.getControlOptions(filtered);
mapOptions = angular.extend(options, controlOptions);
void 0;
for (var group in lazyInitMap) {
var groupMembers = lazyInitMap[group]; //e.g. markers
if (typeof groupMembers == 'object') {
for (var id in groupMembers) {
vm.addObject(group, groupMembers[id]);
}
}
}
vm.map.showInfoWindow = vm.showInfoWindow;
vm.map.hideInfoWindow = vm.hideInfoWindow;
}
// set options
mapOptions.zoom = (mapOptions.zoom && !isNaN(mapOptions.zoom)) ? +mapOptions.zoom : 15;
var center = mapOptions.center;
var exprRegExp = new RegExp(escapeRegExp(exprStartSymbol) + '.*' + escapeRegExp(exprEndSymbol));
if (!mapOptions.center ||
((typeof center === 'string') && center.match(exprRegExp))
) {
mapOptions.center = new google.maps.LatLng(0, 0);
} else if( (typeof center === 'string') && center.match(/^[0-9.-]*,[0-9.-]*$/) ){
var lat = parseFloat(center.split(',')[0]);
var lng = parseFloat(center.split(',')[1]);
mapOptions.center = new google.maps.LatLng(lat, lng);
} else if (!(center instanceof google.maps.LatLng)) {
var geoCenter = mapOptions.center;
delete mapOptions.center;
NgMap.getGeoLocation(geoCenter, mapOptions.geoLocationOptions).
then(function (latlng) {
vm.map.setCenter(latlng);
var geoCallback = mapOptions.geoCallback;
geoCallback && $parse(geoCallback)($scope);
}, function () {
if (mapOptions.geoFallbackCenter) {
vm.map.setCenter(mapOptions.geoFallbackCenter);
}
});
}
vm.map.setOptions(mapOptions);
// set events
for (var eventName in mapEvents) {
var event = mapEvents[eventName];
var listener = google.maps.event.addListener(vm.map, eventName, event);
vm.eventListeners[eventName] = listener;
}
// set observers
vm.observeAttrSetObj(orgAttrs, $attrs, vm.map);
vm.singleInfoWindow = mapOptions.singleInfoWindow;
google.maps.event.trigger(vm.map, 'resize');
google.maps.event.addListenerOnce(vm.map, "idle", function () {
NgMap.addMap(vm);
if (mapOptions.zoomToIncludeMarkers) {
vm.zoomToIncludeMarkers();
}
//TODO: it's for backward compatibiliy. will be removed
$scope.map = vm.map;
$scope.$emit('mapInitialized', vm.map);
//callback
if ($attrs.mapInitialized) {
$parse($attrs.mapInitialized)($scope, {map: vm.map});
}
});
//add maximum zoom listeners if zoom-to-include-markers and and maximum-zoom are valid attributes
if (mapOptions.zoomToIncludeMarkers && mapOptions.maximumZoom) {
google.maps.event.addListener(vm.map, 'zoom_changed', function() {
if (vm.enableMaximumZoomCheck == true) {
vm.enableMaximumZoomCheck = false;
google.maps.event.addListenerOnce(vm.map, 'bounds_changed', function() {
vm.map.setZoom(Math.min(mapOptions.maximumZoom, vm.map.getZoom()));
});
}
});
}
};
$scope.google = google; //used by $scope.eval to avoid eval()
/**
* get map options and events
*/
var orgAttrs = Attr2MapOptions.orgAttributes($element);
var filtered = Attr2MapOptions.filter($attrs);
var options = Attr2MapOptions.getOptions(filtered, {scope: $scope});
var controlOptions = Attr2MapOptions.getControlOptions(filtered);
var mapOptions = angular.extend(options, controlOptions);
var mapEvents = Attr2MapOptions.getEvents($scope, filtered);
void 0;
Object.keys(mapEvents).length && void 0;
vm.mapOptions = mapOptions;
vm.mapEvents = mapEvents;
vm.eventListeners = {};
if (options.lazyInit) { // allows controlled initialization
// parse angular expression for dynamic ids
if (!!$attrs.id &&
// starts with, at position 0
$attrs.id.indexOf(exprStartSymbol, 0) === 0 &&
// ends with
$attrs.id.indexOf(exprEndSymbol, $attrs.id.length - exprEndSymbol.length) !== -1) {
var idExpression = $attrs.id.slice(2,-2);
var mapId = $parse(idExpression)($scope);
} else {
var mapId = $attrs.id;
}
vm.map = {id: mapId}; //set empty, not real, map
NgMap.addMap(vm);
} else {
vm.initializeMap();
}
//Trigger Resize
if(options.triggerResize) {
google.maps.event.trigger(vm.map, 'resize');
}
$element.bind('$destroy', function() {
NgMapPool.returnMapInstance(vm.map);
NgMap.deleteMap(vm);
});
}; // __MapController
__MapController.$inject = [
'$scope', '$element', '$attrs', '$parse', '$interpolate', 'Attr2MapOptions', 'NgMap', 'NgMapPool', 'escapeRegexpFilter'
];
angular.module('ngMap').controller('__MapController', __MapController);
})();
/**
* @ngdoc directive
* @name bicycling-layer
* @param Attr2Options {service}
* convert html attribute to Google map api options
* @description
* Requires: map directive
* Restrict To: Element
*
* @example
*
* <map zoom="13" center="34.04924594193164, -118.24104309082031">
* <bicycling-layer></bicycling-layer>
* </map>
*/
(function() {
'use strict';
var parser;
var linkFunc = function(scope, element, attrs, mapController) {
mapController = mapController[0]||mapController[1];
var orgAttrs = parser.orgAttributes(element);
var filtered = parser.filter(attrs);
var options = parser.getOptions(filtered, {scope: scope});
var events = parser.getEvents(scope, filtered);
void 0;
var layer = getLayer(options, events);
mapController.addObject('bicyclingLayers', layer);
mapController.observeAttrSetObj(orgAttrs, attrs, layer); //observers
element.bind('$destroy', function() {
mapController.deleteObject('bicyclingLayers', layer);
});
};
var getLayer = function(options, events) {
var layer = new google.maps.BicyclingLayer(options);
for (var eventName in events) {
google.maps.event.addListener(layer, eventName, events[eventName]);
}
return layer;
};
var bicyclingLayer= function(Attr2MapOptions) {
parser = Attr2MapOptions;
return {
restrict: 'E',
require: ['?^map','?^ngMap'],
link: linkFunc
};
};
bicyclingLayer.$inject = ['Attr2MapOptions'];
angular.module('ngMap').directive('bicyclingLayer', bicyclingLayer);
})();
/**
* @ngdoc directive
* @name custom-control
* @param Attr2Options {service} convert html attribute to Google map api options
* @param $compile {service} AngularJS $compile service
* @description
* Build custom control and set to the map with position
*
* Requires: map directive
*
* Restrict To: Element
*
* @attr {String} position position of this control
* i.e. TOP_RIGHT
* @attr {Number} index index of the control
* @example
*
* Example:
* <map center="41.850033,-87.6500523" zoom="3">
* <custom-control id="home" position="TOP_LEFT" index="1">
* <div style="background-color: white;">
* <b>Home</b>
* </div>
* </custom-control>
* </map>
*
*/
(function() {
'use strict';
var parser, NgMap;
var linkFunc = function(scope, element, attrs, mapController, $transclude) {
mapController = mapController[0]||mapController[1];
var filtered = parser.filter(attrs);
var options = parser.getOptions(filtered, {scope: scope});
var events = parser.getEvents(scope, filtered);
var innerScope = scope.$new();
/**
* build a custom control element
*/
var customControlEl = element[0].parentElement.removeChild(element[0]);
var content = $transclude( innerScope, function( clone ) {
element.empty();
element.append( clone );
element.on( '$destroy', function() {
innerScope.$destroy();
});
});
/**
* set events
*/
for (var eventName in events) {
google.maps.event.addDomListener(customControlEl, eventName, events[eventName]);
}
mapController.addObject('customControls', customControlEl);
var position = options.position;
mapController.map.controls[google.maps.ControlPosition[position]].push(customControlEl);
element.bind('$destroy', function() {
mapController.deleteObject('customControls', customControlEl);
});
};
var customControl = function(Attr2MapOptions, _NgMap_) {
parser = Attr2MapOptions, NgMap = _NgMap_;
return {
restrict: 'E',
require: ['?^map','?^ngMap'],
link: linkFunc,
transclude: true
}; // return
};
customControl.$inject = ['Attr2MapOptions', 'NgMap'];
angular.module('ngMap').directive('customControl', customControl);
})();
/**
* @ngdoc directive
* @memberof ngmap
* @name custom-marker
* @param Attr2Options {service} convert html attribute to Google map api options
* @param $timeout {service} AngularJS $timeout
* @description
* Marker with html
* Requires: map directive
* Restrict To: Element
*
* @attr {String} position required, position on map
* @attr {Number} z-index optional
* @attr {Boolean} visible optional
* @example
*
* Example:
* <map center="41.850033,-87.6500523" zoom="3">
* <custom-marker position="41.850033,-87.6500523">
* <div>
* <b>Home</b>
* </div>
* </custom-marker>
* </map>
*
*/
/* global document */
(function() {
'use strict';
var parser, $timeout, $compile, NgMap;
var supportedTransform = (function getSupportedTransform() {
var prefixes = 'transform WebkitTransform MozTransform OTransform msTransform'.split(' ');
var div = document.createElement('div');
for(var i = 0; i < prefixes.length; i++) {
if(div && div.style[prefixes[i]] !== undefined) {
return prefixes[i];
}
}
return false;
})();
var CustomMarker = function(options) {
options = options || {};
this.el = document.createElement('div');
this.el.style.display = 'block';
this.el.style.visibility = "hidden";
this.visible = true;
for (var key in options) { /* jshint ignore:line */
this[key] = options[key];
}
};
var setCustomMarker = function() {
CustomMarker.prototype = new google.maps.OverlayView();
CustomMarker.prototype.setContent = function(html, scope) {
this.el.innerHTML = html;
this.el.style.position = 'absolute';
this.el.style.top = 0;
this.el.style.left = 0;
if (scope) {
$compile(angular.element(this.el).contents())(scope);
}
};
CustomMarker.prototype.getDraggable = function() {
return this.draggable;
};
CustomMarker.prototype.setDraggable = function(draggable) {
this.draggable = draggable;
};
CustomMarker.prototype.getPosition = function() {
return this.position;
};
CustomMarker.prototype.setPosition = function(position) {
position && (this.position = position); /* jshint ignore:line */
var _this = this;
if (this.getProjection() && typeof this.position.lng == 'function') {
void 0;
var setPosition = function() {
if (!_this.getProjection()) { return; }
var posPixel = _this.getProjection().fromLatLngToDivPixel(_this.position);
var x = Math.round(posPixel.x - (_this.el.offsetWidth/2));
var y = Math.round(posPixel.y - _this.el.offsetHeight - 10); // 10px for anchor
if (supportedTransform) {
_this.el.style[supportedTransform] = "translate(" + x + "px, " + y + "px)";
} else {
_this.el.style.left = x + "px";
_this.el.style.top = y + "px";
}
_this.el.style.visibility = "visible";
};
if (_this.el.offsetWidth && _this.el.offsetHeight) {
setPosition();
} else {
//delayed left/top calculation when width/height are not set instantly
$timeout(setPosition, 300);
}
}
};
CustomMarker.prototype.setZIndex = function(zIndex) {
if (zIndex === undefined) return;
(this.zIndex !== zIndex) && (this.zIndex = zIndex); /* jshint ignore:line */
(this.el.style.zIndex !== this.zIndex) && (this.el.style.zIndex = this.zIndex);
};
CustomMarker.prototype.getVisible = function() {
return this.visible;
};
CustomMarker.prototype.setVisible = function(visible) {
if (this.el.style.display === 'none' && visible)
{
this.el.style.display = 'block';
} else if (this.el.style.display !== 'none' && !visible) {
this.el.style.display = 'none';
}
this.visible = visible;
};
CustomMarker.prototype.addClass = function(className) {
var classNames = this.el.className.trim().split(' ');
(classNames.indexOf(className) == -1) && classNames.push(className); /* jshint ignore:line */
this.el.className = classNames.join(' ');
};
CustomMarker.prototype.removeClass = function(className) {
var classNames = this.el.className.split(' ');
var index = classNames.indexOf(className);
(index > -1) && classNames.splice(index, 1); /* jshint ignore:line */
this.el.className = classNames.join(' ');
};
CustomMarker.prototype.onAdd = function() {
this.getPanes().overlayMouseTarget.appendChild(this.el);
};
CustomMarker.prototype.draw = function() {
this.setPosition();
this.setZIndex(this.zIndex);
this.setVisible(this.visible);
};
CustomMarker.prototype.onRemove = function() {
this.el.parentNode.removeChild(this.el);
//this.el = null;
};
};
var linkFunc = function(orgHtml, varsToWatch) {
//console.log('orgHtml', orgHtml, 'varsToWatch', varsToWatch);
return function(scope, element, attrs, mapController) {
mapController = mapController[0]||mapController[1];
var orgAttrs = parser.orgAttributes(element);
var filtered = parser.filter(attrs);
var options = parser.getOptions(filtered, {scope: scope});
var events = parser.getEvents(scope, filtered);
/**
* build a custom marker element
*/
element[0].style.display = 'none';
void 0;
var customMarker = new CustomMarker(options);
// Do we really need a timeout with $scope.$apply() here?
setTimeout(function() { //apply contents, class, and location after it is compiled
scope.$watch('[' + varsToWatch.join(',') + ']', function(newVal, oldVal) {
customMarker.setContent(orgHtml, scope);
}, true);
customMarker.setContent(element[0].innerHTML, scope);
var classNames =
(element[0].firstElementChild) && (element[0].firstElementChild.className || '');
customMarker.class && (classNames += " " + customMarker.class);
customMarker.addClass('custom-marker');
classNames && customMarker.addClass(classNames);
void 0;
if (!(options.position instanceof google.maps.LatLng)) {
NgMap.getGeoLocation(options.position).then(
function(latlng) {
customMarker.setPosition(latlng);
}
);
}
});
void 0;
for (var eventName in events) { /* jshint ignore:line */
google.maps.event.addDomListener(
customMarker.el, eventName, events[eventName]);
}
mapController.addObject('customMarkers', customMarker);
//set observers
mapController.observeAttrSetObj(orgAttrs, attrs, customMarker);
element.bind('$destroy', function() {
//Is it required to remove event listeners when DOM is removed?
mapController.deleteObject('customMarkers', customMarker);
});
}; // linkFunc
};
var customMarkerDirective = function(
_$timeout_, _$compile_, $interpolate, Attr2MapOptions, _NgMap_, escapeRegExp
) {
parser = Attr2MapOptions;
$timeout = _$timeout_;
$compile = _$compile_;
NgMap = _NgMap_;
var exprStartSymbol = $interpolate.startSymbol();
var exprEndSymbol = $interpolate.endSymbol();
var exprRegExp = new RegExp(escapeRegExp(exprStartSymbol) + '([^' + exprEndSymbol.substring(0, 1) + ']+)' + escapeRegExp(exprEndSymbol), 'g');
return {
restrict: 'E',
require: ['?^map','?^ngMap'],
compile: function(element) {
void 0;
setCustomMarker();
element[0].style.display ='none';
var orgHtml = element.html();
var matches = orgHtml.match(exprRegExp);
var varsToWatch = [];
//filter out that contains '::', 'this.'
(matches || []).forEach(function(match) {
var toWatch = match.replace(exprStartSymbol,'').replace(exprEndSymbol,'');
if (match.indexOf('::') == -1 &&
match.indexOf('this.') == -1 &&
varsToWatch.indexOf(toWatch) == -1) {
varsToWatch.push(match.replace(exprStartSymbol,'').replace(exprEndSymbol,''));
}
});
return linkFunc(orgHtml, varsToWatch);
}
}; // return
};// function
customMarkerDirective.$inject =
['$timeout', '$compile', '$interpolate', 'Attr2MapOptions', 'NgMap', 'escapeRegexpFilter'];
angular.module('ngMap').directive('customMarker', customMarkerDirective);
})();
/**
* @ngdoc directive
* @name directions
* @description
* Enable directions on map.
* e.g., origin, destination, draggable, waypoints, etc
*
* Requires: map directive
*
* Restrict To: Element
*
* @attr {String} DirectionsRendererOptions
* [Any DirectionsRendererOptions](https://developers.google.com/maps/documentation/javascript/reference#DirectionsRendererOptions)
* @attr {String} DirectionsRequestOptions
* [Any DirectionsRequest options](https://developers.google.com/maps/documentation/javascript/reference#DirectionsRequest)
* @example
* <map zoom="14" center="37.7699298, -122.4469157">
* <directions
* draggable="true"
* panel="directions-panel"
* travel-mode="{{travelMode}}"
* waypoints="[{location:'kingston', stopover:true}]"
* origin="{{origin}}"
* destination="{{destination}}">
* </directions>
* </map>
*/
/* global document */
(function() {
'use strict';
var NgMap, $timeout, NavigatorGeolocation;
var requestTimeout, routeRequest;
// Delay for each route render to accumulate all requests into a single one
// This is required for simultaneous origin\waypoints\destination change
// 20ms should be enough to merge all request data
var routeRenderDelay = 20;
var getDirectionsRenderer = function(options, events) {
if (options.panel) {
options.panel = document.getElementById(options.panel) ||
document.querySelector(options.panel);
}
var renderer = new google.maps.DirectionsRenderer(options);
for (var eventName in events) {
google.maps.event.addListener(renderer, eventName, events[eventName]);
}
return renderer;
};
var updateRoute = function(renderer, options) {
var directionsService = new google.maps.DirectionsService();
/* filter out valid keys only for DirectionsRequest object*/
var request = options;
request.travelMode = request.travelMode || 'DRIVING';
var validKeys = [
'origin', 'destination', 'travelMode', 'transitOptions', 'unitSystem',
'durationInTraffic', 'waypoints', 'optimizeWaypoints',
'provideRouteAlternatives', 'avoidHighways', 'avoidTolls', 'region'
];
if (request) {
for(var key in request) {
if (request.hasOwnProperty(key)) {
(validKeys.indexOf(key) === -1) && (delete request[key]);
}
}
}
if(request.waypoints) {
// Check for acceptable values
if(!Array.isArray(request.waypoints)) {
delete request.waypoints;
}
}
var showDirections = function(request) {
if (requestTimeout && request) {
if (!routeRequest) {
routeRequest = request;
} else {
for (var attr in request) {
if (request.hasOwnProperty(attr)) {
routeRequest[attr] = request[attr];
}
}
}
} else {
requestTimeout = $timeout(function() {
if (!routeRequest) {
routeRequest = request;
}
directionsService.route(routeRequest, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
renderer.setDirections(response);
// Unset request for the next call
routeRequest = undefined;
}
});
$timeout.cancel(requestTimeout);
// Unset expired timeout for the next call
requestTimeout = undefined;
}, routeRenderDelay);
}
};
if (request && request.origin && request.destination) {
if (request.origin == 'current-location') {
NavigatorGeolocation.getCurrentPosition().then(function(ll) {
request.origin = new google.maps.LatLng(ll.coords.latitude, ll.coords.longitude);
showDirections(request);
});
} else if (request.destination == 'current-location') {
NavigatorGeolocation.getCurrentPosition().then(function(ll) {
request.destination = new google.maps.LatLng(ll.coords.latitude, ll.coords.longitude);
showDirections(request);
});
} else {
showDirections(request);
}
}
};
var directions = function(
Attr2MapOptions, _$timeout_, _NavigatorGeolocation_, _NgMap_) {
var parser = Attr2MapOptions;
NgMap = _NgMap_;
$timeout = _$timeout_;
NavigatorGeolocation = _NavigatorGeolocation_;
var linkFunc = function(scope, element, attrs, mapController) {
mapController = mapController[0]||mapController[1];
var orgAttrs = parser.orgAttributes(element);
var filtered = parser.filter(attrs);
var options = parser.getOptions(filtered, {scope: scope});
var events = parser.getEvents(scope, filtered);
var attrsToObserve = parser.getAttrsToObserve(orgAttrs);
var attrsToObserve = [];
if (!filtered.noWatcher) {
attrsToObserve = parser.getAttrsToObserve(orgAttrs);
}
var renderer = getDirectionsRenderer(options, events);
mapController.addObject('directionsRenderers', renderer);
attrsToObserve.forEach(function(attrName) {
(function(attrName) {
attrs.$observe(attrName, function(val) {
if (attrName == 'panel') {
$timeout(function(){
var panel =
document.getElementById(val) || document.querySelector(val);
void 0;
panel && renderer.setPanel(panel);
});
} else if (options[attrName] !== val) { //apply only if changed
var optionValue = parser.toOptionValue(val, {key: attrName});
void 0;
options[attrName] = optionValue;
updateRoute(renderer, options);
}
});
})(attrName);
});
NgMap.getMap().then(function() {
updateRoute(renderer, options);
});
element.bind('$destroy', function() {
mapController.deleteObject('directionsRenderers', renderer);
});
};
return {
restrict: 'E',
require: ['?^map','?^ngMap'],
link: linkFunc
};
}; // var directions
directions.$inject =
['Attr2MapOptions', '$timeout', 'NavigatorGeolocation', 'NgMap'];
angular.module('ngMap').directive('directions', directions);
})();
/**
* @ngdoc directive
* @name drawing-manager
* @param Attr2Options {service} convert html attribute to Google map api options
* @description
* Requires: map directive
* Restrict To: Element
*
* @example
* Example:
*
* <map zoom="13" center="37.774546, -122.433523" map-type-id="SATELLITE">
* <drawing-manager
* on-overlaycomplete="onMapOverlayCompleted()"
* position="ControlPosition.TOP_CENTER"
* drawingModes="POLYGON,CIRCLE"
* drawingControl="true"
* circleOptions="fillColor: '#FFFF00';fillOpacity: 1;strokeWeight: 5;clickable: false;zIndex: 1;editable: true;" >
* </drawing-manager>
* </map>
*
* TODO: Add remove button.
* currently, for our solution, we have the shapes/markers in our own
* controller, and we use some css classes to change the shape button
* to a remove button (<div>X</div>) and have the remove operation in our own controller.
*/
(function() {
'use strict';
angular.module('ngMap').directive('drawingManager', [
'Attr2MapOptions', function(Attr2MapOptions) {
var parser = Attr2MapOptions;
return {
restrict: 'E',
require: ['?^map','?^ngMap'],
link: function(scope, element, attrs, mapController) {
mapController = mapController[0]||mapController[1];
var filtered = parser.filter(attrs);
var options = parser.getOptions(filtered, {scope: scope});
var controlOptions = parser.getControlOptions(filtered);
var events = parser.getEvents(scope, filtered);
/**
* set options
*/
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: options.drawingmode,
drawingControl: options.drawingcontrol,
drawingControlOptions: controlOptions.drawingControlOptions,
circleOptions:options.circleoptions,
markerOptions:options.markeroptions,
polygonOptions:options.polygonoptions,
polylineOptions:options.polylineoptions,
rectangleOptions:options.rectangleoptions
});
//Observers
attrs.$observe('drawingControlOptions', function (newValue) {
drawingManager.drawingControlOptions = parser.getControlOptions({drawingControlOptions: newValue}).drawingControlOptions;
drawingManager.setDrawingMode(null);
drawingManager.setMap(mapController.map);
});
/**
* set events
*/
for (var eventName in events) {
google.maps.event.addListener(drawingManager, eventName, events[eventName]);
}
mapController.addObject('mapDrawingManager', drawingManager);
element.bind('$destroy', function() {
mapController.deleteObject('mapDrawingManager', drawingManager);
});
}
}; // return
}]);
})();
/**