Skip to content

Commit 3979163

Browse files
committed
Added mapTypeId to gmMap.
1 parent 9053ba0 commit 3979163

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

src/controllers/angulargmMapController.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@
103103
this._map.fitBounds(bounds);
104104
}
105105
}
106+
},
107+
108+
'mapTypeId': {
109+
configurable: true, // for testing so we can mock
110+
get: function() {
111+
return this._map.getMapTypeId();
112+
},
113+
set: function(mapTypeId) {
114+
if (mapTypeId == null)
115+
throw 'mapTypeId was null or unknown';
116+
var changed = this.mapTypeId !== mapTypeId;
117+
if (changed) {
118+
this._map.setMapTypeId(mapTypeId);
119+
}
120+
}
106121
}
107122
});
108123

src/directives/gmMap.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* gm-center="myCenter"
1010
* gm-zoom="myZoom"
1111
* gm-bounds="myBounds"
12+
* gm-map-type-id="myMapTypeId"
1213
* gm-map-options="myMapOptions">
1314
* </gm-map>
1415
* ```
@@ -27,18 +28,21 @@
2728
* + `gm-bounds`: name for a bounds variable in the current scope. Value will
2829
* be a google.maps.LatLngBounds object.
2930
*
31+
* + `gm-map-type-id`: name for a mapTypeId variable in the current scope.
32+
* Value will be a string.
33+
*
3034
* + `gm-map-options`: object in the current scope that is a
3135
* google.maps.MapOptions object. If unspecified, will use the values in
3236
* angulargmDefaults.mapOptions. [angulargmDefaults]{@link module:angulargmDefaults}
3337
* is a service, so it is both injectable and overrideable (using
3438
* $provide.decorator).
3539
*
36-
* All attributes except `gm-map-options` are required. The `gm-center`, `gm-zoom`,
37-
* and `gm-bounds` variables do not have to exist in the current scope--they will
38-
* be created if necessary. All three have bi-directional association, i.e.
39-
* drag or zoom the map and they will update, update them and the map will
40-
* change. However, any initial state of these three variables will be
41-
* ignored.
40+
* All attributes except `gm-map-options` are required. The `gm-center`,
41+
* `gm-zoom`, `gm-bounds`, and `gm-map-type-id` variables do not have to exist in
42+
* the current scope--they will be created if necessary. All three have
43+
* bi-directional association, i.e. drag or zoom the map and they will update,
44+
* update them and the map will change. However, any initial state of these
45+
* three variables will be ignored.
4246
*
4347
* If you need to get a handle on the google.maps.Map object, see
4448
* [angulargmContainer]{@link module:angulargmContainer}
@@ -85,6 +89,7 @@
8589
var hasCenter = false;
8690
var hasZoom = false;
8791
var hasBounds = false;
92+
var hasMapTypeId = false;
8893

8994
if (attrs.hasOwnProperty('gmCenter')) {
9095
hasCenter = true;
@@ -95,10 +100,13 @@
95100
if (attrs.hasOwnProperty('gmBounds')) {
96101
hasBounds = true;
97102
}
103+
if (attrs.hasOwnProperty('gmMapTypeId')) {
104+
hasMapTypeId = true;
105+
}
98106

99107
var updateScope = function() {
100108
$timeout(function () {
101-
if (hasCenter || hasZoom || hasBounds) {
109+
if (hasCenter || hasZoom || hasBounds || hasMapTypeId) {
102110
scope.$apply(function (s) {
103111
if (hasCenter) {
104112
scope.gmCenter = controller.center;
@@ -112,6 +120,9 @@
112120
scope.gmBounds = b;
113121
}
114122
}
123+
if (hasMapTypeId) {
124+
scope.gmMapTypeId = controller.mapTypeId;
125+
}
115126
});
116127
}
117128
});
@@ -121,6 +132,7 @@
121132
controller.addMapListener('zoom_changed', updateScope);
122133
controller.addMapListener('center_changed', updateScope);
123134
controller.addMapListener('bounds_changed', updateScope);
135+
controller.addMapListener('maptypeid_changed', updateScope);
124136
controller.addMapListener('resize', updateScope);
125137

126138
if (hasCenter) {
@@ -154,6 +166,15 @@
154166
});
155167
}
156168

169+
if (hasMapTypeId) {
170+
scope.$watch('gmMapTypeId', function(newValue, oldValue) {
171+
var changed = (newValue !== oldValue);
172+
if (changed && newValue) {
173+
controller.mapTypeId = newValue;
174+
}
175+
});
176+
}
177+
157178
scope.$on('gmMapResize', function(event, gmMapId) {
158179
if (scope.gmMapId() === gmMapId) {
159180
controller.mapTrigger('resize');
@@ -175,6 +196,7 @@
175196
gmCenter: '=',
176197
gmZoom: '=',
177198
gmBounds: '=',
199+
gmMapTypeId: '=',
178200
gmMapOptions: '&',
179201
gmMapId: '&'
180202
},

test/unit/controllers/angulargmMapController.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('angulargmMapController', function() {
4545
expect(mapCtrl.zoom).toEqual(1);
4646
var map = mapCntr.getMap(scope.gmMapId());
4747
expect(mapCtrl.bounds).toEqual(map.getBounds());
48+
expect(mapCtrl.mapTypeId).toEqual(google.maps.MapTypeId.TERRAIN);
4849
});
4950

5051

test/unit/directives/gmMapSpec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ describe('gmMap', function() {
1111

1212
beforeEach(inject(function($rootScope, $compile, angulargmContainer, angulargmUtils) {
1313
// compile angulargm directive
14-
elm = angular.element('<gm-map gm-map-id="mapId" gm-center="pCenter" gm-zoom="pZoom" gm-bounds="pBounds" gm-map-options="mapOptions">' +
14+
elm = angular.element('<gm-map gm-map-id="mapId" gm-center="pCenter" gm-zoom="pZoom" gm-bounds="pBounds" gm-map-type-id="pMapTypeId" gm-map-options="mapOptions">' +
1515
'</gm-map>');
1616

1717
scope = $rootScope.$new();
1818
scope.mapOptions = {
1919
center: new google.maps.LatLng(1, 2),
20-
zoom: 3
20+
zoom: 3,
21+
mapTypeId: google.maps.MapTypeId.TERRAIN
2122
}
2223
scope.mapId = 'test';
2324
$compile(elm)(scope);
@@ -31,6 +32,7 @@ describe('gmMap', function() {
3132
new google.maps.LatLng(4, 5),
3233
new google.maps.LatLng(6, 7)
3334
);
35+
initMapTypeId = google.maps.MapTypeId.TERRAIN;
3436

3537
// get MapController
3638
mapCtrl = elm.controller('gmMap');
@@ -49,11 +51,16 @@ describe('gmMap', function() {
4951
get: function() { return bounds;},
5052
set: function(newB) {bounds = newB;},
5153
},
54+
'mapTypeId': {
55+
get: function() { return mapTypeId;},
56+
set: function(newM) {mapTypeId = newM;},
57+
}
5258
});
5359

5460
mapCtrl.center = initCenter;
5561
mapCtrl.zoom = initZoom;
5662
mapCtrl.bounds = initBounds;
63+
mapCtrl.mapTypeId = initMapTypeId;
5764
}));
5865

5966

@@ -99,6 +106,7 @@ describe('gmMap', function() {
99106
expect(scope.pCenter).toEqual(initCenter);
100107
expect(scope.pZoom).toEqual(initZoom);
101108
expect(scope.pBounds).toEqual(initBounds);
109+
expect(scope.pMapTypeId).toEqual(initMapTypeId);
102110
}));
103111

104112

@@ -109,13 +117,15 @@ describe('gmMap', function() {
109117
southWest: {lat: 11, lng: 12},
110118
northEast: {lat: 13, lng: 14}
111119
};
120+
scope.pMapTypeId = 'wut';
112121

113122
google.maps.event.trigger(map, 'bounds_changed');
114123
$timeout.flush();
115124

116125
expect(scope.pCenter).not.toEqual(initCenter);
117126
expect(scope.pZoom).not.toEqual(initZoom);
118127
expect(scope.pBounds).not.toEqual(initBounds);
128+
expect(scope.pMapTypeId).not.toEqual(initMapTypeId);
119129
}));
120130

121131

@@ -190,10 +200,12 @@ describe('gmMap', function() {
190200
scope.pCenter = null;
191201
scope.pZoom = null;
192202
scope.pBounds = null;
203+
scope.pMapTypeId = null;
193204
scope.$digest();
194205
expect(mapCtrl.center).not.toEqual(null);
195206
expect(mapCtrl.zoom).not.toEqual(null);
196207
expect(mapCtrl.bounds).not.toEqual(null);
208+
expect(mapCtrl.mapTypeId).not.toEqual(null);
197209
});
198210

199211

0 commit comments

Comments
 (0)