Skip to content

Commit f83a42f

Browse files
committed
feat(google-maps): Add layer components
Add a base class for the transit and bicycling layers.
1 parent a3e9cb7 commit f83a42f

File tree

3 files changed

+71
-41
lines changed

3 files changed

+71
-41
lines changed

src/google-maps/map-base-layer.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
10+
/// <reference types="googlemaps" />
11+
12+
import {Directive, NgZone, OnDestroy, OnInit} from '@angular/core';
13+
14+
import {GoogleMap} from './google-map/google-map';
15+
16+
@Directive({
17+
selector: 'map-base-layer',
18+
exportAs: 'mapBaseLayer',
19+
})
20+
export class MapBaseLayer implements OnInit, OnDestroy {
21+
constructor(protected readonly _map: GoogleMap, protected readonly _ngZone: NgZone) {}
22+
23+
ngOnInit() {
24+
if (this._map._isBrowser) {
25+
this._ngZone.runOutsideAngular(() => {
26+
this._initializeObject();
27+
});
28+
this._assertInitialized();
29+
this._setMap();
30+
}
31+
}
32+
33+
ngOnDestroy() {
34+
this._unsetMap();
35+
}
36+
37+
private _assertInitialized() {
38+
if (!this._map.googleMap) {
39+
throw Error(
40+
'Cannot access Google Map information before the API has been initialized. ' +
41+
'Please wait for the API to load before trying to interact with it.');
42+
}
43+
}
44+
45+
protected _initializeObject() {}
46+
protected _setMap() {}
47+
protected _unsetMap() {}
48+
}

src/google-maps/map-bicycling-layer/map-bicycling-layer.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
1010
/// <reference types="googlemaps" />
1111

12-
import {Directive, NgZone, OnDestroy, OnInit} from '@angular/core';
12+
import {Directive} from '@angular/core';
1313

14-
import {GoogleMap} from '../google-map/google-map';
14+
import {MapBaseLayer} from '../map-base-layer';
1515

1616
/**
1717
* Angular component that renders a Google Maps Bicycling Layer via the Google Maps JavaScript API.
@@ -22,43 +22,34 @@ import {GoogleMap} from '../google-map/google-map';
2222
selector: 'map-bicycling-layer',
2323
exportAs: 'mapBicyclingLayer',
2424
})
25-
export class MapBicyclingLayer implements OnInit, OnDestroy {
25+
export class MapBicyclingLayer extends MapBaseLayer {
2626
/**
2727
* The underlying google.maps.BicyclingLayer object.
2828
*
2929
* See developers.google.com/maps/documentation/javascript/reference/map#BicyclingLayer
3030
*/
3131
bicyclingLayer?: google.maps.BicyclingLayer;
3232

33-
constructor(private readonly _map: GoogleMap, private readonly _ngZone: NgZone) {}
33+
protected _initializeObject() {
34+
this.bicyclingLayer = new google.maps.BicyclingLayer();
35+
}
3436

35-
ngOnInit() {
36-
if (this._map._isBrowser) {
37-
// Create the object outside the zone so its events don't trigger change detection.
38-
this._ngZone.runOutsideAngular(() => {
39-
this.bicyclingLayer = new google.maps.BicyclingLayer();
40-
});
41-
this._assertInitialized();
42-
this.bicyclingLayer.setMap(this._map.googleMap!);
43-
}
37+
protected _setMap() {
38+
this._assertLayerInitialized();
39+
this.bicyclingLayer.setMap(this._map.googleMap!);
4440
}
4541

46-
ngOnDestroy() {
42+
protected _unsetMap() {
4743
if (this.bicyclingLayer) {
4844
this.bicyclingLayer.setMap(null);
4945
}
5046
}
5147

52-
private _assertInitialized(): asserts this is {bicyclingLayer: google.maps.BicyclingLayer} {
53-
if (!this._map.googleMap) {
54-
throw Error(
55-
'Cannot access Google Map information before the API has been initialized. ' +
56-
'Please wait for the API to load before trying to interact with it.');
57-
}
48+
private _assertLayerInitialized(): asserts this is {bicyclingLayer: google.maps.BicyclingLayer} {
5849
if (!this.bicyclingLayer) {
5950
throw Error(
6051
'Cannot interact with a Google Map Bicycling Layer before it has been initialized. ' +
61-
'Please wait for the Bicycling Layer to load before trying to interact with it.');
52+
'Please wait for the Transit Layer to load before trying to interact with it.');
6253
}
6354
}
6455
}

src/google-maps/map-transit-layer/map-transit-layer.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
1010
/// <reference types="googlemaps" />
1111

12-
import {Directive, NgZone, OnDestroy, OnInit} from '@angular/core';
12+
import {Directive} from '@angular/core';
1313

14-
import {GoogleMap} from '../google-map/google-map';
14+
import {MapBaseLayer} from '../map-base-layer';
1515

1616
/**
1717
* Angular component that renders a Google Maps Transit Layer via the Google Maps JavaScript API.
@@ -22,39 +22,30 @@ import {GoogleMap} from '../google-map/google-map';
2222
selector: 'map-transit-layer',
2323
exportAs: 'mapTransitLayer',
2424
})
25-
export class MapTransitLayer implements OnInit, OnDestroy {
25+
export class MapTransitLayer extends MapBaseLayer {
2626
/**
2727
* The underlying google.maps.TransitLayer object.
2828
*
2929
* See developers.google.com/maps/documentation/javascript/reference/map#TransitLayer
3030
*/
3131
transitLayer?: google.maps.TransitLayer;
3232

33-
constructor(private readonly _map: GoogleMap, private readonly _ngZone: NgZone) {}
33+
protected _initializeObject() {
34+
this.transitLayer = new google.maps.TransitLayer();
35+
}
3436

35-
ngOnInit() {
36-
if (this._map._isBrowser) {
37-
// Create the object outside the zone so its events don't trigger change detection.
38-
this._ngZone.runOutsideAngular(() => {
39-
this.transitLayer = new google.maps.TransitLayer();
40-
});
41-
this._assertInitialized();
42-
this.transitLayer.setMap(this._map.googleMap!);
43-
}
37+
protected _setMap() {
38+
this._assertLayerInitialized();
39+
this.transitLayer.setMap(this._map.googleMap!);
4440
}
4541

46-
ngOnDestroy() {
42+
protected _unsetMap() {
4743
if (this.transitLayer) {
4844
this.transitLayer.setMap(null);
4945
}
5046
}
5147

52-
private _assertInitialized(): asserts this is {transitLayer: google.maps.TransitLayer} {
53-
if (!this._map.googleMap) {
54-
throw Error(
55-
'Cannot access Google Map information before the API has been initialized. ' +
56-
'Please wait for the API to load before trying to interact with it.');
57-
}
48+
private _assertLayerInitialized(): asserts this is {transitLayer: google.maps.TransitLayer} {
5849
if (!this.transitLayer) {
5950
throw Error(
6051
'Cannot interact with a Google Map Transit Layer before it has been initialized. ' +

0 commit comments

Comments
 (0)