Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 57685f2

Browse files
mpienkowskidoom777
authored andcommitted
fix(GoogleMapsAPIWrapper): run Google Maps Apis outside of Angular (#1714)
Wrap all Google Maps APIs calls in the wrapper with runOutsideAngular fixes: #815
1 parent 3549ccf commit 57685f2

1 file changed

Lines changed: 93 additions & 49 deletions

File tree

packages/core/services/google-maps-api-wrapper.ts

Lines changed: 93 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class GoogleMapsAPIWrapper {
2323
}
2424

2525
createMap(el: HTMLElement, mapOptions: mapTypes.MapOptions): Promise<void> {
26-
return this._zone.runOutsideAngular( () => {
26+
return this._zone.runOutsideAngular(() => {
2727
return this._loader.load().then(() => {
2828
const map = new google.maps.Map(el, mapOptions);
2929
this._mapResolver(map as mapTypes.GoogleMap);
@@ -33,73 +33,89 @@ export class GoogleMapsAPIWrapper {
3333
}
3434

3535
setMapOptions(options: mapTypes.MapOptions) {
36-
this._map.then((m: mapTypes.GoogleMap) => { m.setOptions(options); });
36+
return this._zone.runOutsideAngular(() => {
37+
this._map.then((m: mapTypes.GoogleMap) => { m.setOptions(options); });
38+
});
3739
}
3840

3941
/**
4042
* Creates a google map marker with the map context
4143
*/
4244
createMarker(options: mapTypes.MarkerOptions = {} as mapTypes.MarkerOptions, addToMap: boolean = true):
4345
Promise<mapTypes.Marker> {
44-
return this._map.then((map: mapTypes.GoogleMap) => {
45-
if (addToMap) {
46-
options.map = map;
47-
}
48-
return new google.maps.Marker(options);
46+
return this._zone.runOutsideAngular(() => {
47+
return this._map.then((map: mapTypes.GoogleMap) => {
48+
if (addToMap) {
49+
options.map = map;
50+
}
51+
return new google.maps.Marker(options);
52+
});
4953
});
5054
}
5155

5256
createInfoWindow(options?: mapTypes.InfoWindowOptions): Promise<mapTypes.InfoWindow> {
53-
return this._map.then(() => { return new google.maps.InfoWindow(options); });
57+
return this._zone.runOutsideAngular(() => {
58+
return this._map.then(() => { return new google.maps.InfoWindow(options); });
59+
});
5460
}
5561

5662
/**
5763
* Creates a google.map.Circle for the current map.
5864
*/
5965
createCircle(options: mapTypes.CircleOptions): Promise<mapTypes.Circle> {
60-
return this._map.then((map: mapTypes.GoogleMap) => {
61-
if (typeof options.strokePosition === 'string') {
62-
options.strokePosition = google.maps.StrokePosition[options.strokePosition];
63-
}
64-
options.map = map;
65-
return new google.maps.Circle(options);
66+
return this._zone.runOutsideAngular(() => {
67+
return this._map.then((map: mapTypes.GoogleMap) => {
68+
if (typeof options.strokePosition === 'string') {
69+
options.strokePosition = google.maps.StrokePosition[options.strokePosition];
70+
}
71+
options.map = map;
72+
return new google.maps.Circle(options);
73+
});
6674
});
6775
}
6876

6977
/**
7078
* Creates a google.map.Rectangle for the current map.
7179
*/
7280
createRectangle(options: mapTypes.RectangleOptions): Promise<mapTypes.Rectangle> {
73-
return this._map.then((map: mapTypes.GoogleMap) => {
74-
options.map = map;
75-
return new google.maps.Rectangle(options);
81+
return this._zone.runOutsideAngular(() => {
82+
return this._map.then((map: mapTypes.GoogleMap) => {
83+
options.map = map;
84+
return new google.maps.Rectangle(options);
85+
});
7686
});
7787
}
7888

7989
createPolyline(options: PolylineOptions): Promise<Polyline> {
80-
return this.getNativeMap().then((map: mapTypes.GoogleMap) => {
81-
let line = new google.maps.Polyline(options);
82-
line.setMap(map);
83-
return line;
90+
return this._zone.runOutsideAngular(() => {
91+
return this.getNativeMap().then((map: mapTypes.GoogleMap) => {
92+
let line = new google.maps.Polyline(options);
93+
line.setMap(map);
94+
return line;
95+
});
8496
});
8597
}
8698

8799
createPolygon(options: mapTypes.PolygonOptions): Promise<mapTypes.Polygon> {
88-
return this.getNativeMap().then((map: mapTypes.GoogleMap) => {
89-
let polygon = new google.maps.Polygon(options);
90-
polygon.setMap(map);
91-
return polygon;
100+
return this._zone.runOutsideAngular(() => {
101+
return this.getNativeMap().then((map: mapTypes.GoogleMap) => {
102+
let polygon = new google.maps.Polygon(options);
103+
polygon.setMap(map);
104+
return polygon;
105+
});
92106
});
93107
}
94108

95109
/**
96110
* Creates a new google.map.Data layer for the current map
97111
*/
98112
createDataLayer(options?: mapTypes.DataOptions): Promise<mapTypes.Data> {
99-
return this._map.then(m => {
100-
let data = new google.maps.Data(options);
101-
data.setMap(m);
102-
return data;
113+
return this._zone.runOutsideAngular(() => {
114+
return this._map.then(m => {
115+
let data = new google.maps.Data(options);
116+
data.setMap(m);
117+
return data;
118+
});
103119
});
104120
}
105121

@@ -109,10 +125,12 @@ export class GoogleMapsAPIWrapper {
109125
* @returns {Promise<TransitLayer>} a new transit layer object
110126
*/
111127
createTransitLayer(options: mapTypes.TransitLayerOptions): Promise<mapTypes.TransitLayer>{
112-
return this._map.then((map: mapTypes.GoogleMap) => {
113-
let newLayer: mapTypes.TransitLayer = new google.maps.TransitLayer();
114-
newLayer.setMap(options.visible ? map : null);
115-
return newLayer;
128+
return this._zone.runOutsideAngular(() => {
129+
return this._map.then((map: mapTypes.GoogleMap) => {
130+
let newLayer: mapTypes.TransitLayer = new google.maps.TransitLayer();
131+
newLayer.setMap(options.visible ? map : null);
132+
return newLayer;
133+
});
116134
});
117135
}
118136

@@ -122,10 +140,12 @@ export class GoogleMapsAPIWrapper {
122140
* @returns {Promise<BicyclingLayer>} a new bicycling layer object
123141
*/
124142
createBicyclingLayer(options: mapTypes.BicyclingLayerOptions): Promise<mapTypes.BicyclingLayer>{
125-
return this._map.then((map: mapTypes.GoogleMap) => {
126-
let newLayer: mapTypes.BicyclingLayer = new google.maps.BicyclingLayer();
127-
newLayer.setMap(options.visible ? map : null);
128-
return newLayer;
143+
return this._zone.runOutsideAngular(() => {
144+
return this._map.then((map: mapTypes.GoogleMap) => {
145+
let newLayer: mapTypes.BicyclingLayer = new google.maps.BicyclingLayer();
146+
newLayer.setMap(options.visible ? map : null);
147+
return newLayer;
148+
});
129149
});
130150
}
131151

@@ -145,47 +165,71 @@ export class GoogleMapsAPIWrapper {
145165
}
146166

147167
clearInstanceListeners() {
148-
this._map.then((map: mapTypes.GoogleMap) => {
149-
google.maps.event.clearInstanceListeners(map);
168+
return this._zone.runOutsideAngular(() => {
169+
this._map.then((map: mapTypes.GoogleMap) => {
170+
google.maps.event.clearInstanceListeners(map);
171+
});
150172
});
151173
}
152174

153175
setCenter(latLng: mapTypes.LatLngLiteral): Promise<void> {
154-
return this._map.then((map: mapTypes.GoogleMap) => map.setCenter(latLng));
176+
return this._zone.runOutsideAngular(() => {
177+
return this._map.then((map: mapTypes.GoogleMap) => map.setCenter(latLng));
178+
});
155179
}
156180

157-
getZoom(): Promise<number> { return this._map.then((map: mapTypes.GoogleMap) => map.getZoom()); }
181+
getZoom(): Promise<number> {
182+
return this._zone.runOutsideAngular(() => {
183+
return this._map.then((map: mapTypes.GoogleMap) => map.getZoom());
184+
});
185+
}
158186

159187
getBounds(): Promise<mapTypes.LatLngBounds> {
160-
return this._map.then((map: mapTypes.GoogleMap) => map.getBounds());
188+
return this._zone.runOutsideAngular(() => {
189+
return this._map.then((map: mapTypes.GoogleMap) => map.getBounds());
190+
});
161191
}
162192

163193
getMapTypeId(): Promise<mapTypes.MapTypeId> {
164-
return this._map.then((map: mapTypes.GoogleMap) => map.getMapTypeId());
194+
return this._zone.runOutsideAngular(() => {
195+
return this._map.then((map: mapTypes.GoogleMap) => map.getMapTypeId());
196+
});
165197
}
166198

167199
setZoom(zoom: number): Promise<void> {
168-
return this._map.then((map: mapTypes.GoogleMap) => map.setZoom(zoom));
200+
return this._zone.runOutsideAngular(() => {
201+
return this._map.then((map: mapTypes.GoogleMap) => map.setZoom(zoom));
202+
});
169203
}
170204

171205
getCenter(): Promise<mapTypes.LatLng> {
172-
return this._map.then((map: mapTypes.GoogleMap) => map.getCenter());
206+
return this._zone.runOutsideAngular(() => {
207+
return this._map.then((map: mapTypes.GoogleMap) => map.getCenter());
208+
});
173209
}
174210

175211
panTo(latLng: mapTypes.LatLng | mapTypes.LatLngLiteral): Promise<void> {
176-
return this._map.then((map) => map.panTo(latLng));
212+
return this._zone.runOutsideAngular(() => {
213+
return this._map.then((map) => map.panTo(latLng));
214+
});
177215
}
178216

179217
panBy(x: number, y: number): Promise<void> {
180-
return this._map.then((map) => map.panBy(x, y));
218+
return this._zone.runOutsideAngular(() => {
219+
return this._map.then((map) => map.panBy(x, y));
220+
});
181221
}
182222

183223
fitBounds(latLng: mapTypes.LatLngBounds | mapTypes.LatLngBoundsLiteral, padding?: number | mapTypes.Padding): Promise<void> {
184-
return this._map.then((map) => map.fitBounds(latLng, padding));
224+
return this._zone.runOutsideAngular(() => {
225+
return this._map.then((map) => map.fitBounds(latLng, padding));
226+
});
185227
}
186228

187229
panToBounds(latLng: mapTypes.LatLngBounds | mapTypes.LatLngBoundsLiteral, padding?: number | mapTypes.Padding): Promise<void> {
188-
return this._map.then((map) => map.panToBounds(latLng, padding));
230+
return this._zone.runOutsideAngular(() => {
231+
return this._map.then((map) => map.panToBounds(latLng, padding));
232+
});
189233
}
190234

191235
/**

0 commit comments

Comments
 (0)