-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathangular-gm.js
More file actions
2041 lines (1834 loc) · 61.8 KB
/
angular-gm.js
File metadata and controls
2041 lines (1834 loc) · 61.8 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
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
/**
* AngularGM - Google Maps Directives for AngularJS
* @version v2.0.0 - 2017-01-04
* @link http://dylanfprice.github.com/angular-gm
* @author Dylan Price <the.dylan.price@gmail.com>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
/**
* @doc module
* @name angulargm
*
* @description
* Module for embedding Google Maps into AngularJS applications.
*
* # Example Plunkers ([fullscreen](http://embed.plnkr.co/PYDYjVuRHaJpdntoJtqL))
*
* <iframe style="width: 100%; height: 400px" src="http://embed.plnkr.co/PYDYjVuRHaJpdntoJtqL" frameborder="0" allowfullscreen="allowfullscreen">
* </iframe>
*
* Author: Dylan Price <the.dylan.price@gmail.com>
*/
(function() {
'use strict';
angular.module('AngularGM', []).
/**
* @ngdoc service
* @name angulargm.service:angulargmDefaults
*
* @description
* Default configuration.
*
* To provide your own default config, use the following
* ```js
* angular.module('myModule').config(function($provide) {
* $provide.decorator('angulargmDefaults', function($delegate) {
* return angular.extend($delegate, {
* 'precision': 3,
* 'markerConstructor': myCustomMarkerConstructor,
* 'polylineConstructor': myCustomPolylineConstructor,
* 'mapOptions': {
* center: {lat: 55, lng: 111},
* mapTypeId: google.maps.MapTypeId.SATELLITE,
* ...
* }
* });
* });
* });
* ```
*/
factory('angulargmDefaults', function() {
return {
'precision': 3,
'markerConstructor': google.maps.Marker,
'polylineConstructor': google.maps.Polyline,
'circleConstructor': google.maps.Circle,
'mapOptions': {
zoom : 8,
center : {lat: 46, lng: -120},
mapTypeId : google.maps.MapTypeId.ROADMAP
}
};
});
})();
/**
* @ngdoc directive
* @name angulargm.directive:gmCircles
* @element ANY
*
* @description
* A directive for adding circles to a `gmMap`. You may have multiple per `gmMap`.
*
* To use, you specify an array of custom objects and tell the directive how to
* extract the center point for the circle from them. A circle will be created
* for each of your objects. If you assign a new array to your scope variable
* or change the array's length, the circles will also update. The one case
* where `gmCircles` can not automatically detect changes to your objects is
* when you mutate objects in the array. To inform the directive of such
* changes, see the `gmCirclesUpdate` event below.
*
* Only the `gm-objects`, `gm-id` and `gm-circle-center` attributes are required.
*
* @param {expression} gm-objects an array of objects in the current scope.
* These can be any objects you wish to attach to circles, the only requirement
* is that they have a uniform method of accessing an id and a center.
*
* @param {expression} gm-circle-center an angular expression that given an
* object from `gm-objects`, evaluates to a google.maps.LatLngLiteral object.
* Your object can be accessed through the variable `object`. For example, if
* your controller has
* ```js
* ...
* $scope.myObjects = [
* { id: 0, center: { lat: 5, lng: 5}},
* { id: 1, center: { lat: 6, lng: 6}}
* ]
* ...
* ```
* then in the `gm-circles` directive you would put
* ```js
* ...
* gm-objects="myObjects"
* gm-circle-center="object.center"
* ...
* ```
*
* @param {expression} gm-circle-options an angular expression that given
* an object from `gm-objects`, evaluates to a
* [google.maps.CircleOptions](https://developers.google.com/maps/documentation/javascript/reference#CircleOptions)
* object. Your object can be accessed through the variable `object`. If
* unspecified, google maps api defaults will be used.
*
* @param {expression} gm-events a variable in the current scope that is used to
* simulate events on circles. Setting this variable to an object of the form
* ```js
* [
* {
* event: 'click',
* ids: [id1, ...]
* },
* ...
* ]
* ```
* will generate the named events on the circles with the given ids, if a
* circle with each id exists. Note: when setting the `gm-events` variable, you
* must set it to a new object for the changes to be detected. Code like
* ```js
* myEvents[0]["ids"] = [0]
* ```
* will not work.
*
*
* @param {expression} gm-on-*event* an angular expression which evaluates to
* an event handler. This handler will be attached to each circle's \*event\*
* event. The variables 'object' and 'circle' evaluate to your object and the
* [google.maps.circle](https://developers.google.com/maps/documentation/javascript/reference#Circle),
* respectively. For example:
* ```html
* gm-on-click="myClickFn(object, circle)"
* ```
* will call your `myClickFn` whenever a circle is clicked. You may have
* multiple `gm-on-*event*` handlers, but only one for each type of event.
* For events that have an underscore in their name, such as
* 'position_changed', write it as 'gm-on-position-changed'.
*/
/**
* @ngdoc event
* @name angulargm.directive:gmCircles#gmCirclesUpdate
* @eventOf angulargm.directive:gmCircles
* @eventType listen on current gmCircles scope
*
* @description Manually tell the `gmCircles` directive to update the circles.
* This is useful to tell the directive when an object from `gm-objects` is
* mutated--`gmCircles` can not pick up on such changes automatically.
*
* @param {string} objects Not required. The name of the scope variable which
* holds the objects to update circles for, i.e. what you set `gm-objects` to.
* It is useful because there may be multiple instances of the `gmCircles`
* directive. If not specified, all instances of `gmCircles` which are child
* scopes will update their circles.
*
* @example
* ```js
* $scope.$broadcast('gmCirclesUpdate', 'myObjects');
* ```
*/
/**
* @ngdoc event
* @name angulargm.directive:gmCircles#gmCirclesRedraw
* @eventOf angulargm.directive:gmCircles
* @eventType listen on current gmCircles scope
*
* @description Force the gmCircles directive to clear and redraw all circles.
*
* @param {string} objects Not required. The name of the scope variable which
* holds the objects to redraw circles for, i.e. what you set `gm-objects` to.
* It is useful because there may be multiple instances of the `gmCircles`
* directive. If not specified, all instances of gmCircles which are child
* scopes will redraw their circles.
*
* @example
* ```js
* $scope.$broadcast('gmCirclesRedraw', 'myObjects');
* ```
*/
/**
* @ngdoc event
* @name angulargm.directive:gmCircles#gmCirclesUpdated
* @eventOf angulargm.directive:gmCircles
* @eventType emit on current gmCircles scope
*
* @description Emitted when circles are updated.
*
* @param {string} objects the name of the scope variable which holds the
* objects the gmCircles directive was constructed with. This is what
* `gm-objects` was set to.
*
* @example
* ```js
* $scope.$on('gmCirclesUpdated', function(event, objects) {
* if (objects === 'myObjects') {
* ...
* }
* });
* ```
*/
(function () {
'use strict';
angular.module('AngularGM').
directive('gmCircles', ['$parse', '$compile', '$timeout', '$log', 'angulargmUtils', 'angulargmShape',
function ($parse, $compile, $timeout, $log, angulargmUtils, angulargmShape) {
var validateLatLng = angulargmUtils.validateLatLng;
function link(scope, element, attrs, controller) {
if (!('gmCircleCenter' in attrs)) {
throw 'gmCircleCenter attribute required';
}
var circleOptions = function (object) {
var latLngObj = scope.gmCircleCenter({ object: object });
var center = validateLatLng(latLngObj);
if (center == null) {
return null;
}
var circleOptions = scope.gmCircleOptions({ object: object });
var options = {};
angular.extend(options, circleOptions, { center: center });
return options;
};
angulargmShape.createShapeDirective(
'circle', scope, attrs, controller, circleOptions
);
}
return {
restrict: 'AE',
priority: 100,
scope: {
gmObjects: '&',
gmId: '&',
gmCircleCenter: '&',
gmCircleOptions: '&'
},
require: '^gmMap',
link: link
};
}]);
})();
/**
* @ngdoc directive
* @name angulargm.directive:gmInfoWindow
* @element ANY
*
* @description
* A directive for creating a google.maps.InfoWindow.
*
* @param {expression} gm-info-window scope variable to store the
* [google.maps.InfoWindow](https://developers.google.com/maps/documentation/javascript/reference#InfoWindow)
* in. Does not have to already exist.
*
* @param {expression} gm-info-window-options object in the current scope
* that is a
* [google.maps.InfoWindowOptions](https://developers.google.com/maps/documentation/javascript/reference#InfoWindowOptions)
* object. If unspecified, google maps api defaults will be used.
*
* @param {expression} gm-on-*event* an angular expression which evaluates to an
* event handler. This handler will be attached to the InfoWindow's \*event\*
* event. The variable `infoWindow` evaluates to the InfoWindow. For example:
* ```html
* gm-on-closeclick="myCloseclickFn(infoWindow)"
* ```
* will call your myCloseclickFn whenever the InfoWindow is clicked closed. You
* may have multiple `gm-on-*event*` handlers, but only one for each type of
* event.
*/
(function () {
'use strict';
angular.module('AngularGM').
/*
* Much of this code is taken from the Angular UI team, see:
* https://github.com/angular-ui/ui-map/blob/master/ui-map.js
*/
directive('gmInfoWindow',
['$parse', '$compile', '$timeout', 'angulargmUtils',
function ($parse, $compile, $timeout, angulargmUtils) {
/** aliases */
var getEventHandlers = angulargmUtils.getEventHandlers;
function link(scope, element, attrs, controller) {
var opts = angular.extend({}, scope.$eval(attrs.gmInfoWindowOptions));
opts.content = element[0];
var model = $parse(attrs.gmInfoWindow);
var infoWindow = model(scope);
if (!infoWindow) {
infoWindow = new google.maps.InfoWindow(opts);
model.assign(scope, infoWindow);
}
var handlers = getEventHandlers(attrs);
// set up info window event handlers
angular.forEach(handlers, function(handler, event) {
google.maps.event.addListener(infoWindow, event, function() {
$timeout(function() {
handler(scope, {
infoWindow: infoWindow
});
});
});
});
}
return {
restrict: 'A',
priority: 100,
scope: false,
link: link
};
}]);
})();
/**
* @ngdoc directive
* @name angulargm.directive:gmMap
* @element ANY
*
* @description
* A directive for embedding google maps into your app.
*
* `gm-map-id` is required. The `gm-center`, `gm-zoom`, `gm-bounds`, and
* `gm-map-type-id` variables do not have to exist in the current scope--they
* will be created if necessary. All three have bi-directional association,
* i.e. drag or zoom the map and they will update, update them and the map
* will change. However, any initial state of these three variables will be
* ignored.
*
* If you need to get a handle on the google.maps.Map object, see
* {@link angulargm.service:angulargmContainer angulargmContainer}
*
* @param {expression} gm-map-id angular expression that evaluates to a unique
* string id for the map, e.g. `'map_canvas'` or `myMapId` where myMapId is a
* variable in the current scope. This allows you to have multiple
* maps/instances of the directive.
*
*
* @param {expression} gm-center center variable in the current scope. The
* value will be a google.maps.LatLngLiteral object.
*
*
* @param {expression} gm-zoom zoom variable in the current scope. Value will
* be an integer.
*
*
* @param {expression} gm-bounds bounds variable in the current scope. Value
* will be a google.maps.LatLngBounds object.
*
*
* @param {expression} gm-map-type-id mapTypeId variable in the current scope.
* Value will be a string.
*
*
* @param {expression} gm-map-options object in the current scope that is a
* google.maps.MapOptions object. If unspecified, will use the values in
* angulargmDefaults.mapOptions. {@link angulargm.service:angulargmDefaults angulargmDefaults} is a service, so it is
* both injectable and overrideable (using $provide.decorator).
*
* @param {expression} gm-on-*event* an angular expression which evaluates to
* an event handler. This handler will be attached to each marker's \*event\*
* event. The variables 'map' and 'event' evaluate to the map and the
* [google.maps.MouseEvent](https://developers.google.com/maps/documentation/javascript/reference#MouseEvent),
* respectively. The map is always passed in, but the MouseEvent is only passed in if the event emits it. For example:
* ```html
* gm-on-click="myClickFn(map, event)"
* ```
* will call your `myClickFn` whenever the map is clicked. You may have
* multiple `gm-on-*event*` handlers, but only one for each type of event. For events that have an underscore in their
* name, such as 'center_changed', write it as 'gm-on-center-changed'.
*
*
*/
/**
* @ngdoc event
* @name angulargm.directive:gmMap#gmMapResize
* @eventOf angulargm.directive:gmMap
* @eventType listen on current gmMap scope
*
* @description Alias for google.maps.event.trigger(map, 'resize')
*
* @param {string} mapId Required. The id of your map.
* @example
* ```js
* $scope.$broadcast('gmMapResize', 'myMapId')
* ```
*/
/**
* @ngdoc event
* @name angulargm.directive:gmMap#gmMapIdle
* @eventOf angulargm.directive:gmMap
* @eventType emit on current gmMap scope
*
* @description Emitted when the map is finished loading (when the map fires
* the 'idle' event).
*
* @param {string} mapId the id of the map which finished loading.
*
* @example
* ```js
* $scope.$on('gmMapIdle', function(event, mapId) {
* if (mapId === 'myMapId') {
* ...
* }
* });
* ```
*/
(function () {
'use strict';
angular.module('AngularGM').
directive('gmMap', ['$timeout', 'angulargmUtils', 'debounce', function ($timeout, angulargmUtils, debounce) {
/** aliases **/
var getEventHandlers = angulargmUtils.getEventHandlers;
var validateLatLng = angulargmUtils.validateLatLng;
var latLngLiteralEqual = angulargmUtils.latLngLiteralEqual;
/** link function **/
function link(scope, element, attrs, controller) {
// initialize scope
if (!angular.isDefined(scope.gmCenter)) {
scope.center = {};
}
if (!angular.isDefined(scope.gmBounds)) {
scope.bounds = {};
}
// Make sure gmMapId is defined
// Note: redundant check in MapController. Can't hurt.
if (!angular.isDefined(scope.gmMapId)) {
throw 'angulargm must have non-empty gmMapId attribute';
}
// Check what's defined in attrs
// Note: this is also redundant since angular will throw an exception if
// these attributes are not set. I may make these optional in the future
// (pending angular support).
var hasCenter = false;
var hasZoom = false;
var hasBounds = false;
var hasMapTypeId = false;
if (attrs.hasOwnProperty('gmCenter')) {
hasCenter = true;
}
if (attrs.hasOwnProperty('gmZoom')) {
hasZoom = true;
}
if (attrs.hasOwnProperty('gmBounds')) {
hasBounds = true;
}
if (attrs.hasOwnProperty('gmMapTypeId')) {
hasMapTypeId = true;
}
var _updateScope = function() {
$timeout(function () {
if (hasCenter || hasZoom || hasBounds || hasMapTypeId) {
scope.$apply(function (s) {
if (hasCenter) {
scope.gmCenter = controller.center;
}
if (hasZoom) {
scope.gmZoom = controller.zoom;
}
if (hasBounds) {
var b = controller.bounds;
if (b) {
scope.gmBounds = b;
}
}
if (hasMapTypeId) {
scope.gmMapTypeId = controller.mapTypeId;
}
});
}
});
};
var updateScope = debounce(_updateScope, 100);
// Add event listeners to the map
controller.addMapListener('drag', updateScope);
controller.addMapListener('zoom_changed', updateScope);
controller.addMapListener('center_changed', updateScope);
controller.addMapListener('bounds_changed', updateScope);
controller.addMapListener('maptypeid_changed', updateScope);
controller.addMapListener('resize', updateScope);
// Add user supplied callbacks
var map = controller.getMap(attrs.gmMapId);
var handlers = getEventHandlers(attrs); // map events -> handlers
angular.forEach(handlers, function(handler, event) {
controller.addMapListener(event, function(ev) {
// pass the map in
var locals = {
map: map
};
// And optionally a MouseEvent object if it exists
if (ev !== undefined) {
locals.event = ev;
}
$timeout(function() {
handler(scope.$parent, locals);
});
});
});
if (hasCenter) {
scope.$watch('gmCenter', function (newValue, oldValue) {
var changed = (
validateLatLng(newValue) != null &&
!latLngLiteralEqual(newValue, oldValue)
);
if (changed && !controller.dragging) {
controller.center = newValue;
}
}, true);
}
if (hasZoom) {
scope.$watch('gmZoom', function (newValue, oldValue) {
var ok = (newValue != null && !isNaN(newValue));
if (ok && newValue !== oldValue) {
controller.zoom = newValue;
}
});
}
if (hasBounds) {
scope.$watch('gmBounds', function(newValue, oldValue) {
var changed = (newValue !== oldValue);
if (changed && !controller.dragging) {
var bounds = newValue;
if (bounds)
controller.bounds = bounds;
}
});
}
if (hasMapTypeId) {
scope.$watch('gmMapTypeId', function(newValue, oldValue) {
var changed = (newValue !== oldValue);
if (changed && newValue) {
controller.mapTypeId = newValue;
}
});
}
scope.$on('gmMapResize', function(event, gmMapId) {
if (scope.gmMapId() === gmMapId) {
controller.mapTrigger('resize');
}
});
controller.addMapListenerOnce('idle', function() {
scope.$emit('gmMapIdle', scope.gmMapId());
});
controller.mapTrigger('resize');
}
return {
restrict: 'AE',
priority: 100,
template: '<div>' +
'<div id="" style="width:100%;height:100%;"></div>' +
'<div ng-transclude></div>' +
'</div>',
transclude: true,
replace: true,
scope: {
gmCenter: '=',
gmZoom: '=',
gmBounds: '=',
gmMapTypeId: '=',
gmMapOptions: '&',
gmMapId: '&'
},
controller: 'angulargmMapController',
link: link
};
}]);
})();
/**
* @ngdoc directive
* @name angulargm.directive:gmMarkers
* @element ANY
*
* @description
* A directive for adding markers to a `gmMap`. You may have multiple per `gmMap`.
*
* To use, you specify an array of custom objects and tell the directive how to
* extract an id and position from them. A marker will be created for each of
* your objects. If you assign a new array to your scope variable or change the
* array's length (i.e. add or remove an object), the markers will also update.
* The one case where `gmMarkers` can not automatically detect changes to your
* objects is when you mutate objects in the array. To inform the directive of
* such changes, see the `gmMarkersUpdate` event below.
*
* Only the `gm-objects`, `gm-id` and `gm-position` attributes are required.
*
* @param {expression} gm-objects an array of objects in the current scope.
* These can be any objects you wish to attach to markers, the only requirement
* is that they have a uniform method of accessing an id and a position.
*
* @param {expression} gm-id an angular expression that given an object from
* `gm-objects`, evaluates to a unique identifier for that object. Your object
* can be accessed through the variable `object`. See `gm-position` below for
* an example.
*
* @param {expression} gm-position an angular expression that given an object
* from `gm-objects`, evaluates to google.maps.LatLngLiteral object. Your
* object can be accessed through the variable `object`. For example, if your
* controller has
* ```js
* ...
* $scope.myObjects = [
* { id: 0, location: { lat: 5, lng: 5} },
* { id: 1, location: { lat: 6, lng: 6} }
* ]
* ...
* ```
* then in the `gm-markers` directive you would put
* ```js
* ...
* gm-objects="myObjects"
* gm-id="object.id"
* gm-position="object.location"
* ...
* ```
*
*
* @param {expression} gm-marker-options an angular expression that given
* an object from `gm-objects`, evaluates to a
* [google.maps.MarkerOptions](https://developers.google.com/maps/documentation/javascript/reference#MarkerOptions)
* object. Your object can be accessed through the variable `object`. If
* unspecified, google maps api defaults will be used.
*
*
* @param {expression} gm-events a variable in the current scope that is used to
* simulate events on markers. Setting this variable to an object of the form
* ```js
* [
* {
* event: 'click',
* ids: [id1, ...]
* },
* ...
* ]
* ```
* will generate the named events on the markers with the given ids, if a
* marker with each id exists. Note: when setting the `gm-events` variable, you
* must set it to a new object for the changes to be detected. Code like
* ```js
* myEvents[0]["ids"] = [0]
* ```
* will not work.
*
*
* @param {expression} gm-on-*event* an angular expression which evaluates to
* an event handler. This handler will be attached to each marker's \*event\*
* event. The variables 'object' and 'marker' evaluate to your object and the
* [google.maps.Marker](https://developers.google.com/maps/documentation/javascript/reference#Marker),
* respectively. For example:
* ```html
* gm-on-click="myClickFn(object, marker)"
* ```
* will call your `myClickFn` whenever a marker is clicked. You may have
* multiple `gm-on-*event*` handlers, but only one for each type of event.
* For events that have an underscore in their name, such as
* 'position_changed', write it as 'gm-on-position-changed'.
*/
/**
* @ngdoc event
* @name angulargm.directive:gmMarkers#gmMarkersUpdate
* @eventOf angulargm.directive:gmMarkers
* @eventType listen on current gmMarkers scope
*
* @description Manually tell the `gmMarkers` directive to update the markers.
* This is useful to tell the directive when an object from `gm-objects` is
* mutated--`gmMarkers` can not pick up on such changes automatically.
*
* @param {string} objects Not required. The name of the scope variable which
* holds the objects to update markers for, i.e. what you set `gm-objects` to.
* It is useful because there may be multiple instances of the `gmMarkers`
* directive. If not specified, all instances of `gmMarkers` which are child
* scopes will update their markers.
*
* @example
* ```js
* $scope.$broadcast('gmMarkersUpdate', 'myObjects');
* ```
*/
/**
* @ngdoc event
* @name angulargm.directive:gmMarkers#gmMarkersRedraw
* @eventOf angulargm.directive:gmMarkers
* @eventType listen on current gmMarkers scope
*
* @description Force the `gmMarkers` directive to clear and redraw all markers.
*
* @param {string} objects Not required. The name of the scope variable which
* holds the objects to redraw markers for, i.e. what you set `gm-objects` to.
* It is useful because there may be multiple instances of the `gmMarkers`
* directive. If not specified, all instances of `gmMarkers` which are child
* scopes will redraw their markers.
*
* @example
* ```js
* $scope.$broadcast('gmMarkersRedraw', 'myObjects');
* ```
*/
/**
* @ngdoc event
* @name angulargm.directive:gmMarkers#gmMarkersUpdated
* @eventOf angulargm.directive:gmMarkers
* @eventType emit on current gmMarkers scope
*
* @description Emitted when markers are updated.
*
* @param {string} objects the name of the scope variable which holds the
* objects the `gmMarkers` directive was constructed with. This is what
* `gm-objects` was set to.
*
* @example
* ```js
* $scope.$on('gmMarkersUpdated', function(event, objects) {
* if (objects === 'myObjects') {
* ...
* }
* });
* ```
*/
(function () {
'use strict';
angular.module('AngularGM').
directive('gmMarkers',
['$log', '$parse', '$timeout', 'angulargmUtils', 'angulargmShape',
function($log, $parse, $timeout, angulargmUtils, angulargmShape) {
/** aliases */
var validateLatLng = angulargmUtils.validateLatLng;
function link(scope, element, attrs, controller) {
// check marker attrs
if (!('gmPosition' in attrs)) {
throw 'gmPosition attribute required';
}
var markerOptions = function(object) {
var latLngObj = scope.gmPosition({object: object});
var position = validateLatLng(latLngObj);
if (position == null) {
return null;
}
var markerOptions = scope.gmMarkerOptions({object: object});
var options = {};
angular.extend(options, markerOptions, {position: position});
return options;
};
angulargmShape.createShapeDirective(
'marker', scope, attrs, controller, markerOptions
);
}
return {
restrict: 'AE',
priority: 100,
scope: {
gmObjects: '&',
gmId: '&',
gmPosition: '&',
gmMarkerOptions: '&',
gmEvents: '&'
},
require: '^gmMap',
link: link
};
}]);
})();
/**
* @ngdoc directive
* @name angulargm.directive:gmPolylines
* @element ANY
*
* @description
* A directive for adding polylines to a `gmMap`. You may have multiple per `gmMap`.
*
* To use, you specify an array of custom objects and tell the directive how to
* extract location data from them. A polyline will be created for each of your
* objects. If you assign a new array to your scope variable or change the
* array's length, the polylines will also update. The one case where
* `gmPolylines` can not automatically detect changes to your objects is when
* you mutate objects in the array. To inform the directive of such changes,
* see the `gmPolylinesUpdate` event below.
*
* Only the `gm-objects`, `gm-id` and `gm-path` attributes are required.
*
* @param {expression} gm-objects an array of objects in the current scope.
* These can be any objects you wish to attach to polylines, the only requirement
* is that they have a uniform method of accessing an id and a path.
*
* @param {expression} gm-path an angular expression that given an object from
* `gm-objects`, evaluates to an array of google.maps.LatLngLiteral objects.
* Your object can be accessed through the variable `object`. For example, if
* your controller has
* ```js
* ...
* $scope.myObjects = [
* { id: 0, path: [ { lat: 5, lng: 5}, {lat: 4, lng: 4} ]},
* { id: 1, path: [ { lat: 6, lng: 6}, {lat: 7, lng: 7} ]}
* ]
* ...
* ```
* then in the `gm-polylines` directive you would put
* ```js
* ...
* gm-objects="myObjects"
* gm-get-path="object.path"
* ...
* ```
*
* @param {expression} gm-polyline-options an angular expression that given
* an object from `gm-objects`, evaluates to a
* [google.maps.PolylineOptions](https://developers.google.com/maps/documentation/javascript/reference#PolylineOptions)
* object. Your object can be accessed through the variable `object`. If
* unspecified, google maps api defaults will be used.
*
* @param {expression} gm-events a variable in the current scope that is used to
* simulate events on polylines. Setting this variable to an object of the form
* ```js
* [
* {
* event: 'click',
* ids: [id1, ...]
* },
* ...
* ]
* ```
* will generate the named events on the polylines with the given ids, if a
* polyline with each id exists. Note: when setting the `gm-events` variable, you
* must set it to a new object for the changes to be detected. Code like
* ```js
* myEvents[0]["ids"] = [0]
* ```
* will not work.
*
*
* @param {expression} gm-on-*event* an angular expression which evaluates to
* an event handler. This handler will be attached to each polyline's \*event\*
* event. The variables 'object' and 'polyline' evaluate to your object and the
* [google.maps.Polyline](https://developers.google.com/maps/documentation/javascript/reference#Polyline),
* respectively. For example:
* ```html
* gm-on-click="myClickFn(object, polyline)"
* ```
* will call your `myClickFn` whenever a polyline is clicked. You may have
* multiple `gm-on-*event*` handlers, but only one for each type of event.
* For events that have an underscore in their name, such as
* 'position_changed', write it as 'gm-on-position-changed'.
*/
/**
* @ngdoc event
* @name angulargm.directive:gmPolylines#gmPolylinesUpdate
* @eventOf angulargm.directive:gmPolylines
* @eventType listen on current gmPolylines scope
*
* @description Manually tell the `gmPolylines` directive to update the polylines.
* This is useful to tell the directive when an object from `gm-objects` is
* mutated--`gmPolylines` can not pick up on such changes automatically.
*
* @param {string} objects Not required. The name of the scope variable which
* holds the objects to update polylines for, i.e. what you set `gm-objects` to.
* It is useful because there may be multiple instances of the `gmPolylines`
* directive. If not specified, all instances of `gmPolylines` which are child
* scopes will update their polylines.
*
* @example
* ```js
* $scope.$broadcast('gmPolylinesUpdate', 'myObjects');
* ```
*/
/**
* @ngdoc event
* @name angulargm.directive:gmPolylines#gmPolylinesRedraw
* @eventOf angulargm.directive:gmPolylines
* @eventType listen on current gmPolylines scope
*
* @description Force the gmPolylines directive to clear and redraw all polylines.
*
* @param {string} objects Not required. The name of the scope variable which
* holds the objects to redraw polylines for, i.e. what you set `gm-objects` to.
* It is useful because there may be multiple instances of the `gmPolylines`
* directive. If not specified, all instances of gmPolylines which are child
* scopes will redraw their polylines.
*
* @example
* ```js
* $scope.$broadcast('gmPolylinesRedraw', 'myObjects');
* ```
*/
/**
* @ngdoc event
* @name angulargm.directive:gmPolylines#gmPolylinesUpdated
* @eventOf angulargm.directive:gmPolylines
* @eventType emit on current gmPolylines scope
*
* @description Emitted when polylines are updated.
*
* @param {string} objects the name of the scope variable which holds the
* objects the gmPolylines directive was constructed with. This is what
* `gm-objects` was set to.
*
* @example
* ```js
* $scope.$on('gmPolylinesUpdated', function(event, objects) {
* if (objects === 'myObjects') {
* ...
* }
* });
* ```
*/
(function () {
'use strict';
angular.module('AngularGM').
directive('gmPolylines', ['$parse', '$compile', '$timeout', '$log', 'angulargmUtils', 'angulargmShape',
function ($parse, $compile, $timeout, $log, angulargmUtils, angulargmShape) {
/** aliases */
var validateLatLng = angulargmUtils.validateLatLng;
function link(scope, element, attrs, controller) {
if (!('gmPath' in attrs)) {
throw 'gmPath attribute required';
}
var polylineOptions = function(object) {
var lineLatLngs = scope.gmPath({object: object});
var path = [];
angular.forEach(lineLatLngs, function(latlng) {
var position = validateLatLng(latlng);
if (position == null) {
$log.warn('Unable to generate lat/lng from ', latlng);
return;
}
path.push(position);
});
var polylineOptions = scope.gmPolylineOptions({object: object});
var options = {};