Skip to content

Commit d9ec9bd

Browse files
committed
feat(google-maps): Add map-info-window component
Add component that implements the Google Maps JavaScript API InfoWindow that displays on the Google Map and can be anchored to a marker.
1 parent b778173 commit d9ec9bd

17 files changed

+549
-11
lines changed

src/dev-app/google-map/google-map-demo.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
(mapMousemove)="handleMove($event)"
88
(mapRightclick)="handleRightclick()">
99
<map-marker></map-marker>
10-
<map-marker *ngFor="let markerPosition of markerPositions"
11-
[position]="markerPosition"
12-
[options]="markerOptions"
13-
(mapClick)="clickMarker($event)"></map-marker>
10+
<map-marker #marker
11+
*ngFor="let markerPosition of markerPositions"
12+
[position]="markerPosition"
13+
[options]="markerOptions"
14+
(mapClick)="clickMarker(marker)"></map-marker>
15+
<map-info-window [position]="infoWindowPosition">Testing 1 2 3</map-info-window>
1416
</google-map>
1517

1618
<div>Latitude: {{display?.lat}}</div>

src/dev-app/google-map/google-map-demo.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Component} from '@angular/core';
109
import {HttpClient} from '@angular/common/http';
10+
import {Component, ViewChild} from '@angular/core';
11+
import {MapInfoWindow, MapMarker} from '@angular/google-maps';
1112

1213
/** Demo Component for @angular/google-maps/map */
1314
@Component({
@@ -16,11 +17,14 @@ import {HttpClient} from '@angular/common/http';
1617
templateUrl: 'google-map-demo.html',
1718
})
1819
export class GoogleMapDemo {
20+
@ViewChild(MapInfoWindow, {static: false}) infoWindow: MapInfoWindow;
21+
1922
isReady = false;
2023

2124
center = {lat: 24, lng: 12};
2225
markerOptions = {draggable: false};
2326
markerPositions: google.maps.LatLngLiteral[] = [];
27+
infoWindowPosition: google.maps.LatLngLiteral;
2428
zoom = 4;
2529
display?: google.maps.LatLngLiteral;
2630

@@ -39,9 +43,8 @@ export class GoogleMapDemo {
3943
this.display = event.latLng.toJSON();
4044
}
4145

42-
clickMarker(event: google.maps.MouseEvent) {
43-
console.log(this.markerOptions);
44-
this.markerOptions = {draggable: true};
46+
clickMarker(marker: MapMarker) {
47+
this.infoWindow.open(marker);
4548
}
4649

4750
handleRightclick() {

src/google-maps/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ng_module(
1212
module_name = "@angular/google-maps",
1313
deps = [
1414
"//src/google-maps/google-map",
15+
"//src/google-maps/map-info-window",
1516
"//src/google-maps/map-marker",
1617
"@npm//@angular/core",
1718
"@npm//@types/googlemaps",

src/google-maps/google-map/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ng_module(
1414
exclude = ["**/*.spec.ts"],
1515
),
1616
deps = [
17+
"//src/google-maps/map-info-window",
1718
"//src/google-maps/map-marker",
1819
"@npm//@angular/core",
1920
"@npm//@types/googlemaps",
@@ -29,6 +30,7 @@ ng_test_library(
2930
),
3031
deps = [
3132
":google-map",
33+
"//src/google-maps/map-info-window",
3234
"//src/google-maps/map-marker",
3335
"//src/google-maps/testing",
3436
"@npm//@angular/platform-browser",

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

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

5+
import {MapInfoWindow, MapInfoWindowModule} from '../map-info-window/index';
56
import {MapMarker, MapMarkerModule} from '../map-marker/index';
67
import {
78
createMapConstructorSpy,
@@ -40,6 +41,7 @@ describe('GoogleMap', () => {
4041
TestBed.configureTestingModule({
4142
imports: [
4243
GoogleMapModule,
44+
MapInfoWindowModule,
4345
MapMarkerModule,
4446
],
4547
declarations: [TestApp],
@@ -254,6 +256,19 @@ describe('GoogleMap', () => {
254256

255257
expect(markerComponent._setMap).toHaveBeenCalledWith(mapSpy);
256258
});
259+
260+
it('calls setMap on child info window components', () => {
261+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
262+
createMapConstructorSpy(mapSpy).and.callThrough();
263+
264+
const fixture = TestBed.createComponent(TestApp);
265+
const infoWindowComponent =
266+
fixture.debugElement.query(By.directive(MapInfoWindow)).componentInstance;
267+
spyOn(infoWindowComponent, '_setMap').and.callThrough();
268+
fixture.detectChanges();
269+
270+
expect(infoWindowComponent._setMap).toHaveBeenCalledWith(mapSpy);
271+
});
257272
});
258273

259274
@Component({
@@ -267,6 +282,7 @@ describe('GoogleMap', () => {
267282
(centerChanged)="handleCenterChanged()"
268283
(mapRightclick)="handleRightclick($event)">
269284
<map-marker></map-marker>
285+
<map-info-window>test content</map-info-window>
270286
</google-map>`,
271287
})
272288
class TestApp {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import {BehaviorSubject, combineLatest, Observable, Subject} from 'rxjs';
1616
import {map, shareReplay, take, takeUntil} from 'rxjs/operators';
1717

18+
import {MapInfoWindow} from '../map-info-window';
1819
import {MapMarker} from '../map-marker/index';
1920

2021
interface GoogleMapsWindow extends Window {
@@ -179,6 +180,8 @@ export class GoogleMap implements OnChanges, OnInit, AfterContentInit, OnDestroy
179180

180181
@ContentChildren(MapMarker) _markers: QueryList<MapMarker>;
181182

183+
@ContentChildren(MapInfoWindow) _infoWindows: QueryList<MapInfoWindow>;
184+
182185
private _mapEl: HTMLElement;
183186
private _googleMap!: UpdatedGoogleMap;
184187

@@ -227,7 +230,11 @@ export class GoogleMap implements OnChanges, OnInit, AfterContentInit, OnDestroy
227230
for (const marker of this._markers.toArray()) {
228231
marker._setMap(this._googleMap);
229232
}
233+
for (const infoWindow of this._infoWindows.toArray()) {
234+
infoWindow._setMap(this._googleMap);
235+
}
230236
this._watchForMarkerChanges();
237+
this._watchForInfoWindowChanges();
231238
}
232239

233240
ngOnDestroy() {
@@ -471,4 +478,14 @@ export class GoogleMap implements OnChanges, OnInit, AfterContentInit, OnDestroy
471478
}
472479
});
473480
}
481+
482+
private _watchForInfoWindowChanges() {
483+
combineLatest(this._googleMapChanges, this._infoWindows.changes)
484+
.pipe(takeUntil(this._destroy))
485+
.subscribe(([googleMap, infoWindows]) => {
486+
for (let infoWindow of infoWindows) {
487+
infoWindow._setMap(googleMap);
488+
}
489+
});
490+
}
474491
}

src/google-maps/google-maps-module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88

99
import {NgModule} from '@angular/core';
1010

11-
import {MapMarker, MapMarkerModule} from './map-marker/index';
1211
import {GoogleMap, GoogleMapModule} from './google-map/index';
12+
import {MapInfoWindow, MapInfoWindowModule} from './map-info-window/index';
13+
import {MapMarker, MapMarkerModule} from './map-marker/index';
1314

1415
@NgModule({
1516
imports: [
1617
GoogleMapModule,
18+
MapInfoWindowModule,
1719
MapMarkerModule,
1820
],
1921
exports: [
2022
GoogleMap,
23+
MapInfoWindow,
2124
MapMarker,
2225
],
2326
})
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary")
4+
load(
5+
"//tools:defaults.bzl",
6+
"ng_module",
7+
"ng_test_library",
8+
"ng_web_test_suite",
9+
)
10+
11+
ng_module(
12+
name = "map-info-window",
13+
srcs = glob(
14+
["**/*.ts"],
15+
exclude = ["**/*.spec.ts"],
16+
),
17+
assets = [":map-info-window.css"],
18+
deps = [
19+
"//src/google-maps/map-marker",
20+
"@npm//@angular/core",
21+
"@npm//@types/googlemaps",
22+
"@npm//rxjs",
23+
],
24+
)
25+
26+
sass_binary(
27+
name = "map-info-window_scss",
28+
src = "map-info-window.scss",
29+
)
30+
31+
ng_test_library(
32+
name = "unit_test_sources",
33+
srcs = glob(
34+
["**/*.spec.ts"],
35+
exclude = ["**/*.e2e.spec.ts"],
36+
),
37+
deps = [
38+
":map-info-window",
39+
"//src/google-maps/map-marker",
40+
"//src/google-maps/testing",
41+
"@npm//@angular/platform-browser",
42+
],
43+
)
44+
45+
ng_web_test_suite(
46+
name = "unit_tests",
47+
deps = [":unit_test_sources"],
48+
)
49+
50+
filegroup(
51+
name = "source-files",
52+
srcs = glob(["**/*.ts"]),
53+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
export * from './map-info-window';
10+
export * from './map-info-window-module';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
import {NgModule} from '@angular/core';
10+
import {MapInfoWindow} from './map-info-window';
11+
12+
@NgModule({
13+
exports: [MapInfoWindow],
14+
declarations: [MapInfoWindow],
15+
})
16+
export class MapInfoWindowModule {
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:host {
2+
.map-info-window-content {
3+
display: none;
4+
}
5+
}

0 commit comments

Comments
 (0)