Skip to content

Commit

Permalink
Fixes #3. Reuse google map instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanfprice committed Jul 20, 2013
1 parent de8e57f commit bf97b1c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
22 changes: 15 additions & 7 deletions src/controllers/angulargmMapController.js
Expand Up @@ -42,9 +42,9 @@
mapDiv.attr('id', mapId);

var config = this._getConfig($scope, gMDefaults);

// 'private' properties
this._map = this._createMap(mapId, mapDiv, config, gMContainer);
this._map = this._createMap(mapId, mapDiv, config, gMContainer, $scope);
this._markers = {};

// 'public' properties
Expand Down Expand Up @@ -106,7 +106,7 @@
});

this._initDragListeners();
$scope.$on('$destroy', angular.bind(this, this._destroy, mapId));
$scope.$on('$destroy', angular.bind(this, this._destroy));
};


Expand All @@ -127,8 +127,8 @@
map = new google.maps.Map(element[0], config);
gMContainer.addMap(id, map);
} else {
throw 'A map with id ' + id + ' already exists. You must use' +
' different ids for each instance of the angulargm directive.';
var div = map.getDiv();
element.replaceWith(div);
}
return map;
};
Expand All @@ -151,8 +151,16 @@
};


this._destroy = function(mapId) {
gMContainer.removeMap(mapId);
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);
});
});
};


Expand Down
10 changes: 7 additions & 3 deletions src/services/angulargmContainer.js
Expand Up @@ -8,13 +8,15 @@
* method so you can guarantee the map will be initialized. For example,
*
* ```
* function MyCtrl(angulargmContainer) {
* angular.module('myModule').
*
* run(function(angulargmContainer) {
* var gmapPromise = angulargmContainer.getMapPromise('myMapid');
*
* gmapPromise.then(function(gmap) {
* // google map configuration here
* });
* }
* });
* ```
*
* @module angulargmContainer
Expand Down Expand Up @@ -74,7 +76,9 @@
/**
* Removes map with given mapId from this container, and deletes the map.
* In order for this to work you must ensure there are no references to the
* map object.
* map object. Note: this will likely cause a memory leak, see
* http://stackoverflow.com/questions/10485582/what-is-the-proper-way-to-destroy-a-map-instance
*
* @param {string} mapId the unique id of the map to remove
* @method
*/
Expand Down
8 changes: 6 additions & 2 deletions test/unit/controllers/angulargmMapController.js
Expand Up @@ -57,10 +57,14 @@ describe('angulargmMapController', function() {
}));


it('destroys the map on scope destroy', function() {
it('removes markers on scope destroy', function() {
var mapId = scope.gmMapId();
scope.$destroy();
expect(mapCntr.getMap(mapId)).toBeUndefined();
numMarkers = 0;
mapCtrl.forEachMarker(function(marker) {
numMarkers++;
});
expect(numMarkers).toEqual(0);
});


Expand Down

0 comments on commit bf97b1c

Please sign in to comment.