Skip to content

Commit 94c71b0

Browse files
mbehrlichjelbourn
authored andcommitted
feat(google-maps): Add methods and event handling to Google Map (#16804)
* 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. * feat(google-maps): Add methods and event handling to Google Map Change event names so they will not collide with native angular events. * feat(google-maps): Add methods and event handling to Google Map Add jsdoc and comments to new events, methods, and properties.
1 parent 784756d commit 94c71b0

File tree

6 files changed

+472
-16
lines changed

6 files changed

+472
-16
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
height="400px"
33
width="750px"
44
[center]="center"
5-
[zoom]="zoom"></google-map>
5+
[zoom]="zoom"
6+
(mapClick)="handleClick($event)"
7+
(mapMousemove)="handleMove($event)"></google-map>
8+
9+
<div>Latitude: {{display?.lat}}</div>
10+
<div>Longitude: {{display?.lng}}</div>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ export class GoogleMapDemo {
2020

2121
center = {lat: 24, lng: 12};
2222
zoom = 4;
23+
display?: google.maps.LatLngLiteral;
2324

2425
constructor(httpClient: HttpClient) {
2526
httpClient.jsonp('https://maps.googleapis.com/maps/api/js?', 'callback')
2627
.subscribe(() => {
2728
this.isReady = true;
2829
});
2930
}
31+
32+
handleClick(event: google.maps.MouseEvent) {
33+
this.center = event.latLng.toJSON();
34+
}
35+
36+
handleMove(event: google.maps.MouseEvent) {
37+
this.display = event.latLng.toJSON();
38+
}
3039
}

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

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,37 @@ 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+
/** Represents boundaries of a map to be used in tests. */
20+
const testBounds: google.maps.LatLngBoundsLiteral = {
21+
east: 12,
22+
north: 13,
23+
south: 14,
24+
west: 15,
25+
};
26+
27+
/** Represents a latitude/longitude position to be used in tests. */
28+
const testPosition: google.maps.LatLngLiteral = {
29+
lat: 30,
30+
lng: 35,
31+
};
32+
1233
describe('GoogleMap', () => {
1334
let mapConstructorSpy: jasmine.Spy;
14-
let mapSpy: jasmine.SpyObj<google.maps.Map>;
35+
let mapSpy: jasmine.SpyObj<UpdatedGoogleMap>;
1536

1637
beforeEach(async(() => {
1738
TestBed.configureTestingModule({
@@ -31,7 +52,7 @@ describe('GoogleMap', () => {
3152

3253
it('throws an error is the Google Maps JavaScript API was not loaded', () => {
3354
mapSpy = createMapSpy(DEFAULT_OPTIONS);
34-
mapConstructorSpy = createMapConstructorSpy(mapSpy, false);
55+
createMapConstructorSpy(mapSpy, false);
3556

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

134242
@Component({
@@ -137,12 +245,21 @@ describe('GoogleMap', () => {
137245
[width]="width"
138246
[center]="center"
139247
[zoom]="zoom"
140-
[options]="options"></google-map>`,
248+
[options]="options"
249+
(mapClick)="handleClick"
250+
(centerChanged)="handleCenterChanged"
251+
(mapRightclick)="handleRightclick"></google-map>`,
141252
})
142253
class TestApp {
143254
height?: string;
144255
width?: string;
145256
center?: google.maps.LatLngLiteral;
146257
zoom?: number;
147258
options?: google.maps.MapOptions;
259+
260+
handleClick(event: google.maps.MouseEvent) {}
261+
262+
handleCenterChanged() {}
263+
264+
handleRightclick(event: google.maps.MouseEvent) {}
148265
}

0 commit comments

Comments
 (0)