88 * @property {MarkerClusterer } markerClusterer MarkerClusterer initiated within `map` directive
99 */
1010/*jshint -W089*/
11+ /* global google, ngMap */
1112ngMap . MapController = function ( ) {
13+ 'use strict' ;
1214
1315 this . map = null ;
14- this . _objects = [ ] ;
16+ this . _objects = [ ] ; /* temporary collection of map objects */
1517
1618 /**
17- * Add a marker to map and $scope.markers
19+ * Add an object to the collection of group
1820 * @memberof MapController
19- * @name addMarker
20- * @param {Marker } marker google map marker
21+ * @name addObject
22+ * @param groupName the name of collection that object belongs to
23+ * @param obj an object to add into a collection, i.e. marker, shape
2124 */
22- this . addMarker = function ( marker ) {
25+ this . addObject = function ( groupName , obj ) {
2326 /**
24- * marker and shape are initialized before map is initialized
25- * so, collect _objects then will init. those when map is initialized
27+ * objects, i.e. markers and shapes, are initialized before map is initialized
28+ * so, we collect those objects, then, we will add to map when map is initialized
2629 * However the case as in ng-repeat, we can directly add to map
2730 */
28- if ( this . map ) {
29- this . map . markers = this . map . markers || { } ;
30- marker . setMap ( this . map ) ;
31- if ( marker . centered ) {
32- this . map . setCenter ( marker . position ) ;
33- }
34- var len = Object . keys ( this . map . markers ) . length ;
35- this . map . markers [ marker . id || len ] = marker ;
36- } else {
37- this . _objects . push ( marker ) ;
38- }
39- } ;
40-
41- /**
42- * Add a shape to map and $scope.shapes
43- * @memberof MapController
44- * @name addShape
45- * @param {Shape } shape google map shape
46- */
47- this . addShape = function ( shape ) {
48- if ( this . map ) {
49- this . map . shapes = this . map . shapes || { } ;
50- shape . setMap ( this . map ) ;
51- var len = Object . keys ( this . map . shapes ) . length ;
52- this . map . shapes [ shape . id || len ] = shape ;
53- } else {
54- this . _objects . push ( shape ) ;
55- }
56- } ;
57-
58- this . addObject = function ( groupName , obj ) {
5931 if ( this . map ) {
6032 this . map [ groupName ] = this . map [ groupName ] || { } ;
6133 var len = Object . keys ( this . map [ groupName ] ) . length ;
6234 this . map [ groupName ] [ obj . id || len ] = obj ;
6335 if ( groupName != "infoWindows" && obj . setMap ) { //infoWindow.setMap works like infoWindow.open
6436 obj . setMap ( this . map ) ;
6537 }
38+ if ( obj . centered && obj . position ) {
39+ this . map . setCenter ( obj . position ) ;
40+ }
6641 } else {
6742 obj . groupName = groupName ;
6843 this . _objects . push ( obj ) ;
6944 }
7045 }
7146
7247 /**
73- * Add a shape to map and $scope.shapes
48+ * Delete an object from the collection and remove from map
49+ * @memgerof MapController
50+ * @name deleteFromMap
51+ * @param {Array } objs the collection of objects. i.e., map.markers
52+ * @param {Object } obj the object to be removed. i.e., marker
53+ */
54+ this . deleteObject = function ( groupName , obj ) {
55+ /* delete from map */
56+ obj . map && obj . setMap ( null ) ;
57+
58+ /* delete from group */
59+ var objs = obj . map [ groupName ] ;
60+ for ( var name in objs ) {
61+ objs [ name ] === obj && ( delete objs [ name ] ) ;
62+ }
63+ } ;
64+
65+ /**
66+ * Add collected objects to map
7467 * @memberof MapController
7568 * @name addShape
7669 * @param {Shape } shape google map shape
@@ -79,13 +72,13 @@ ngMap.MapController = function() {
7972 for ( var i = 0 ; i < objects . length ; i ++ ) {
8073 var obj = objects [ i ] ;
8174 if ( obj instanceof google . maps . Marker ) {
82- this . addMarker ( obj ) ;
75+ this . addObject ( 'markers' , obj ) ;
8376 } else if ( obj instanceof google . maps . Circle ||
8477 obj instanceof google . maps . Polygon ||
8578 obj instanceof google . maps . Polyline ||
8679 obj instanceof google . maps . Rectangle ||
8780 obj instanceof google . maps . GroundOverlay ) {
88- this . addShape ( obj ) ;
81+ this . addObject ( 'shapes' , obj ) ;
8982 } else {
9083 this . addObject ( obj . groupName , obj ) ;
9184 }
0 commit comments