Skip to content

Commit d1d6b65

Browse files
committed
feat(google): Expose the underlying Google Maps objects
Fix merge conflicts and update the Circle component with changes.
1 parent 3179663 commit d1d6b65

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

src/google-maps/map-circle/map-circle.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Component, ViewChild} from '@angular/core';
22
import {async, TestBed} from '@angular/core/testing';
33
import {By} from '@angular/platform-browser';
44

5-
import {DEFAULT_OPTIONS, UpdatedGoogleMap} from '../google-map/google-map';
5+
import {DEFAULT_OPTIONS} from '../google-map/google-map';
66
import {GoogleMapsModule} from '../google-maps-module';
77
import {
88
createCircleConstructorSpy,
@@ -15,7 +15,7 @@ import {
1515
import {MapCircle} from './map-circle';
1616

1717
describe('MapCircle', () => {
18-
let mapSpy: jasmine.SpyObj<UpdatedGoogleMap>;
18+
let mapSpy: jasmine.SpyObj<google.maps.Map>;
1919
let circleCenter: google.maps.LatLngLiteral;
2020
let circleRadius: number;
2121
let circleOptions: google.maps.CircleOptions;

src/google-maps/map-circle/map-circle.ts

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class MapCircle implements OnInit, OnDestroy {
3737
*
3838
* @see developers.google.com/maps/documentation/javascript/reference/polygon#Circle
3939
*/
40-
circle: google.maps.Circle; // initialized in ngOnInit
40+
circle?: google.maps.Circle; // initialized in ngOnInit
4141

4242
@Input()
4343
set options(options: google.maps.CircleOptions) {
@@ -159,77 +159,86 @@ export class MapCircle implements OnInit, OnDestroy {
159159
constructor(private readonly _map: GoogleMap, private readonly _ngZone: NgZone) {}
160160

161161
ngOnInit() {
162-
const combinedOptionsChanges = this._combineOptions();
163-
164-
combinedOptionsChanges.pipe(take(1)).subscribe(options => {
165-
// Create the object outside the zone so its events don't trigger change detection.
166-
// We'll bring it back in inside the `MapEventManager` only for the events that the
167-
// user has subscribed to.
168-
this._ngZone.runOutsideAngular(() => {
169-
this.circle = new google.maps.Circle(options);
162+
if (this._map._isBrowser) {
163+
this._combineOptions().pipe(take(1)).subscribe(options => {
164+
// Create the object outside the zone so its events don't trigger change detection.
165+
// We'll bring it back in inside the `MapEventManager` only for the events that the
166+
// user has subscribed to.
167+
this._ngZone.runOutsideAngular(() => {
168+
this.circle = new google.maps.Circle(options);
169+
});
170+
this._assertInitialized();
171+
this.circle!.setMap(this._map.googleMap!);
172+
this._eventManager.setTarget(this.circle);
170173
});
171-
this.circle.setMap(this._map._googleMap);
172-
this._eventManager.setTarget(this.circle);
173-
});
174174

175-
this._watchForOptionsChanges();
176-
this._watchForCenterChanges();
177-
this._watchForRadiusChanges();
175+
this._watchForOptionsChanges();
176+
this._watchForCenterChanges();
177+
this._watchForRadiusChanges();
178+
}
178179
}
179180

180181
ngOnDestroy() {
181182
this._eventManager.destroy();
182183
this._destroyed.next();
183184
this._destroyed.complete();
184-
this.circle.setMap(null);
185+
if (this.circle) {
186+
this.circle.setMap(null);
187+
}
185188
}
186189

187190
/**
188191
* @see
189192
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getBounds
190193
*/
191194
getBounds(): google.maps.LatLngBounds {
192-
return this.circle.getBounds();
195+
this._assertInitialized();
196+
return this.circle!.getBounds();
193197
}
194198

195199
/**
196200
* @see
197201
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getCenter
198202
*/
199203
getCenter(): google.maps.LatLng {
200-
return this.circle.getCenter();
204+
this._assertInitialized();
205+
return this.circle!.getCenter();
201206
}
202207

203208
/**
204209
* @see
205210
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getDraggable
206211
*/
207212
getDraggable(): boolean {
208-
return this.circle.getDraggable();
213+
this._assertInitialized();
214+
return this.circle!.getDraggable();
209215
}
210216

211217
/**
212218
* @see
213219
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getEditable
214220
*/
215221
getEditable(): boolean {
216-
return this.circle.getEditable();
222+
this._assertInitialized();
223+
return this.circle!.getEditable();
217224
}
218225

219226
/**
220227
* @see
221228
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getCenter
222229
*/
223230
getRadius(): number {
224-
return this.circle.getRadius();
231+
this._assertInitialized();
232+
return this.circle!.getRadius();
225233
}
226234

227235
/**
228236
* @see
229237
* developers.google.com/maps/documentation/javascript/reference/polygon#Circle.getVisible
230238
*/
231239
getVisible(): boolean {
232-
return this.circle.getVisible();
240+
this._assertInitialized();
241+
return this.circle!.getVisible();
233242
}
234243

235244
private _combineOptions(): Observable<google.maps.CircleOptions> {
@@ -246,23 +255,39 @@ export class MapCircle implements OnInit, OnDestroy {
246255

247256
private _watchForOptionsChanges() {
248257
this._options.pipe(takeUntil(this._destroyed)).subscribe(options => {
249-
this.circle.setOptions(options);
258+
this._assertInitialized();
259+
this.circle!.setOptions(options);
250260
});
251261
}
252262

253263
private _watchForCenterChanges() {
254264
this._center.pipe(takeUntil(this._destroyed)).subscribe(center => {
255265
if (center) {
256-
this.circle.setCenter(center);
266+
this._assertInitialized();
267+
this.circle!.setCenter(center);
257268
}
258269
});
259270
}
260271

261272
private _watchForRadiusChanges() {
262273
this._radius.pipe(takeUntil(this._destroyed)).subscribe(radius => {
263274
if (radius !== undefined) {
264-
this.circle.setRadius(radius);
275+
this._assertInitialized();
276+
this.circle!.setRadius(radius);
265277
}
266278
});
267279
}
280+
281+
private _assertInitialized() {
282+
if (!this._map.googleMap) {
283+
throw Error(
284+
'Cannot access Google Map information before the API has been initialized. ' +
285+
'Please wait for the API to load before trying to interact with it.');
286+
}
287+
if (!this.circle) {
288+
throw Error(
289+
'Cannot interact with a Google Map Circle before it has been ' +
290+
'initialized. Please wait for the Circle to load before trying to interact with it.');
291+
}
292+
}
268293
}

0 commit comments

Comments
 (0)