Skip to content

Commit 721f6b3

Browse files
committed
feat(google-maps): Add methods and event handling to Google Map
Add support for Google Maps event handlers, properties, and methods to the Google Map component, including click handlers, methods to pan with the map and access to the controls and data properties.
1 parent 1de1b98 commit 721f6b3

File tree

8 files changed

+308
-17
lines changed

8 files changed

+308
-17
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
height="400px"
33
width="750px"
44
[center]="center"
5-
[zoom]="zoom"></google-map>
5+
[zoom]="zoom"
6+
(click)="handleClick($event)"></google-map>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ export class GoogleMapDemo {
2727
this.isReady = true;
2828
});
2929
}
30+
31+
handleClick(event: google.maps.MouseEvent) {
32+
this.center = event.latLng.toJSON();
33+
}
3034
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ng_module(
1414
["**/*.ts"],
1515
exclude = ["**/*.spec.ts"],
1616
),
17+
assets = glob(["**/*.html"]),
1718
module_name = "@angular/google-maps/google-map",
1819
deps = [
1920
"@npm//@angular/core",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div class="map-container"
2+
(click)="$event.stopPropagation()"
3+
(dblclick)="$event.stopPropagation()"
4+
(drag)="$event.stopPropagation()"
5+
(dragend)="$event.stopPropagation()"
6+
(dragstart)="$event.stopPropagation()"
7+
(mousemove)="$event.stopPropagation()"
8+
(mouseout)="$event.stopPropagation()"
9+
(mouseover)="$event.stopPropagation()"></div>

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

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

5-
import {DEFAULT_HEIGHT, DEFAULT_OPTIONS, DEFAULT_WIDTH, GoogleMapModule} from './index';
5+
import {
6+
DEFAULT_HEIGHT,
7+
DEFAULT_OPTIONS,
8+
DEFAULT_WIDTH,
9+
GoogleMap,
10+
GoogleMapModule,
11+
UpdatedGoogleMap
12+
} from './index';
613
import {
714
createMapConstructorSpy,
815
createMapSpy,
916
TestingWindow
1017
} from './testing/fake-google-map-utils';
1118

19+
const TEST_BOUNDS: google.maps.LatLngBoundsLiteral = {
20+
east: 12,
21+
north: 13,
22+
south: 14,
23+
west: 15,
24+
};
25+
26+
const TEST_POSITION: google.maps.LatLngLiteral = {
27+
lat: 30,
28+
lng: 35,
29+
};
30+
1231
describe('GoogleMap', () => {
1332
let mapConstructorSpy: jasmine.Spy;
14-
let mapSpy: jasmine.SpyObj<google.maps.Map>;
33+
let mapSpy: jasmine.SpyObj<UpdatedGoogleMap>;
1534

1635
beforeEach(async(() => {
1736
TestBed.configureTestingModule({
@@ -31,7 +50,7 @@ describe('GoogleMap', () => {
3150

3251
it('throws an error is the Google Maps JavaScript API was not loaded', () => {
3352
mapSpy = createMapSpy(DEFAULT_OPTIONS);
34-
mapConstructorSpy = createMapConstructorSpy(mapSpy, false);
53+
createMapConstructorSpy(mapSpy, false);
3554

3655
expect(() => TestBed.createComponent(TestApp))
3756
.toThrow(new Error(
@@ -129,6 +148,93 @@ describe('GoogleMap', () => {
129148
const container = fixture.debugElement.query(By.css('div'));
130149
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, correctedOptions);
131150
});
151+
152+
it('exposes methods that change the configuration of the Google Map', () => {
153+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
154+
createMapConstructorSpy(mapSpy).and.callThrough();
155+
156+
const fixture = TestBed.createComponent(TestApp);
157+
fixture.detectChanges();
158+
159+
const component = fixture.debugElement.query(By.directive(GoogleMap)).componentInstance;
160+
161+
component.fitBounds(TEST_BOUNDS, 10);
162+
expect(mapSpy.fitBounds).toHaveBeenCalledWith(TEST_BOUNDS, 10);
163+
164+
component.panBy(12, 13);
165+
expect(mapSpy.panBy).toHaveBeenCalledWith(12, 13);
166+
167+
component.panTo(TEST_POSITION);
168+
expect(mapSpy.panTo).toHaveBeenCalledWith(TEST_POSITION);
169+
170+
component.panToBounds(TEST_BOUNDS, 10);
171+
expect(mapSpy.panToBounds).toHaveBeenCalledWith(TEST_BOUNDS, 10);
172+
});
173+
174+
it('exposes methods that get information about the Google Map', () => {
175+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
176+
createMapConstructorSpy(mapSpy).and.callThrough();
177+
178+
const fixture = TestBed.createComponent(TestApp);
179+
fixture.detectChanges();
180+
181+
const component = fixture.debugElement.query(By.directive(GoogleMap)).componentInstance;
182+
183+
mapSpy.getBounds.and.returnValue(null);
184+
expect(component.getBounds()).toBe(null);
185+
186+
component.getCenter();
187+
expect(mapSpy.getCenter).toHaveBeenCalled();
188+
189+
mapSpy.getClickableIcons.and.returnValue(true);
190+
expect(component.getClickableIcons()).toBe(true);
191+
192+
mapSpy.getHeading.and.returnValue(10);
193+
expect(component.getHeading()).toBe(10);
194+
195+
component.getMapTypeId();
196+
expect(mapSpy.getMapTypeId).toHaveBeenCalled();
197+
198+
mapSpy.getProjection.and.returnValue(null);
199+
expect(component.getProjection()).toBe(null);
200+
201+
component.getStreetView();
202+
expect(mapSpy.getStreetView).toHaveBeenCalled();
203+
204+
mapSpy.getTilt.and.returnValue(7);
205+
expect(component.getTilt()).toBe(7);
206+
207+
mapSpy.getZoom.and.returnValue(5);
208+
expect(component.getZoom()).toBe(5);
209+
});
210+
211+
it('initializes event handlers that are set on the map', () => {
212+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
213+
createMapConstructorSpy(mapSpy).and.callThrough();
214+
215+
const fixture = TestBed.createComponent(TestApp);
216+
fixture.detectChanges();
217+
218+
expect(mapSpy.addListener).toHaveBeenCalledWith('click', jasmine.any(Function));
219+
expect(mapSpy.addListener).toHaveBeenCalledWith('center_changed', jasmine.any(Function));
220+
expect(mapSpy.addListener).toHaveBeenCalledWith('rightclick', jasmine.any(Function));
221+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('bounds_changed', jasmine.any(Function));
222+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('dblclick', jasmine.any(Function));
223+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('drag', jasmine.any(Function));
224+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('dragend', jasmine.any(Function));
225+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('dragstart', jasmine.any(Function));
226+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('heading_changed', jasmine.any(Function));
227+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('idle', jasmine.any(Function));
228+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('maptypeid_changed', jasmine.any(Function));
229+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('mousemove', jasmine.any(Function));
230+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('mouseout', jasmine.any(Function));
231+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('mouseover', jasmine.any(Function));
232+
expect(mapSpy.addListener)
233+
.not.toHaveBeenCalledWith('projection_changed', jasmine.any(Function));
234+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('tilesloaded', jasmine.any(Function));
235+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('tilt_changed', jasmine.any(Function));
236+
expect(mapSpy.addListener).not.toHaveBeenCalledWith('zoom_changed', jasmine.any(Function));
237+
});
132238
});
133239

134240
@Component({
@@ -137,12 +243,21 @@ describe('GoogleMap', () => {
137243
[width]="width"
138244
[center]="center"
139245
[zoom]="zoom"
140-
[options]="options"></google-map>`,
246+
[options]="options"
247+
(click)="handleClick"
248+
(centerChanged)="handleCenterChanged"
249+
(rightclick)="handleRightclick"></google-map>`,
141250
})
142251
class TestApp {
143252
height?: string;
144253
width?: string;
145254
center?: google.maps.LatLngLiteral;
146255
zoom?: number;
147256
options?: google.maps.MapOptions;
257+
258+
handleClick(event: google.maps.MouseEvent) {}
259+
260+
handleCenterChanged() {}
261+
262+
handleRightclick(event: google.maps.MouseEvent) {}
148263
}

0 commit comments

Comments
 (0)