Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(google-maps): support setting the map type #18578

Merged
merged 1 commit into from Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/dev-app/google-map/google-map-demo.html
Expand Up @@ -5,7 +5,8 @@
[zoom]="zoom"
(mapClick)="handleClick($event)"
(mapMousemove)="handleMove($event)"
(mapRightclick)="handleRightclick()">
(mapRightclick)="handleRightclick()"
[mapTypeId]="mapTypeId">
<map-marker #firstMarker [position]="center" (mapClick)="clickMarker(firstMarker)"></map-marker>
<map-marker #marker
*ngFor="let markerPosition of markerPositions"
Expand All @@ -22,6 +23,16 @@
<p><label>Latitude:</label> {{display?.lat}}</p>
<p><label>Longitude:</label> {{display?.lng}}</p>

<div>
<label for="map-type">
Select map type

<select id="map-type" (change)="mapTypeChanged($event)" #mapType>
<option *ngFor="let type of mapTypeIds" [value]="type">{{type}}</option>
</select>
</label>
</div>

<div>
<label for="polyline-checkbox">
Toggle Polyline
Expand Down
12 changes: 12 additions & 0 deletions src/dev-app/google-map/google-map-demo.ts
Expand Up @@ -66,6 +66,14 @@ export class GoogleMapDemo {
circleOptions: google.maps.CircleOptions =
{center: CIRCLE_CENTER, radius: CIRCLE_RADIUS, strokeColor: 'grey', strokeOpacity: 0.8};

mapTypeId: google.maps.MapTypeId;
mapTypeIds = [
google.maps.MapTypeId.HYBRID,
google.maps.MapTypeId.ROADMAP,
google.maps.MapTypeId.SATELLITE,
google.maps.MapTypeId.TERRAIN
];

handleClick(event: google.maps.MouseEvent) {
this.markerPositions.push(event.latLng.toJSON());
}
Expand Down Expand Up @@ -130,4 +138,8 @@ export class GoogleMapDemo {
radius: this.circle.getRadius(),
};
}

mapTypeChanged(event: Event) {
this.mapTypeId = (event.target as HTMLSelectElement).value as unknown as google.maps.MapTypeId;
}
}
30 changes: 27 additions & 3 deletions src/google-maps/google-map/google-map.spec.ts
Expand Up @@ -122,7 +122,7 @@ describe('GoogleMap', () => {
});

it('sets center and zoom of the map', () => {
const options = {center: {lat: 3, lng: 5}, zoom: 7};
const options = {center: {lat: 3, lng: 5}, zoom: 7, mapTypeId: undefined};
mapSpy = createMapSpy(options);
mapConstructorSpy = createMapConstructorSpy(mapSpy).and.callThrough();

Expand All @@ -143,7 +143,7 @@ describe('GoogleMap', () => {
});

it('sets map options', () => {
const options = {center: {lat: 3, lng: 5}, zoom: 7, draggable: false};
const options = {center: {lat: 3, lng: 5}, zoom: 7, draggable: false, mapTypeId: undefined};
mapSpy = createMapSpy(options);
mapConstructorSpy = createMapConstructorSpy(mapSpy).and.callThrough();

Expand All @@ -162,7 +162,12 @@ describe('GoogleMap', () => {

it('gives precedence to center and zoom over options', () => {
const inputOptions = {center: {lat: 3, lng: 5}, zoom: 7, heading: 170};
const correctedOptions = {center: {lat: 12, lng: 15}, zoom: 5, heading: 170};
const correctedOptions = {
center: {lat: 12, lng: 15},
zoom: 5,
heading: 170,
mapTypeId: undefined
};
mapSpy = createMapSpy(correctedOptions);
mapConstructorSpy = createMapConstructorSpy(mapSpy);

Expand Down Expand Up @@ -280,6 +285,23 @@ describe('GoogleMap', () => {
expect(addSpy).toHaveBeenCalledWith('projection_changed', jasmine.any(Function));
subscription.unsubscribe();
});

it('should set the map type', () => {
mapSpy = createMapSpy(DEFAULT_OPTIONS);
mapConstructorSpy = createMapConstructorSpy(mapSpy).and.callThrough();

const fixture = TestBed.createComponent(TestApp);
fixture.componentInstance.mapTypeId = 'terrain' as unknown as google.maps.MapTypeId;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not google.maps.MapTypeId.TERRAIN

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the global google variable isn't available when running unit tests.

fixture.detectChanges();

expect(mapConstructorSpy).toHaveBeenCalledWith(jasmine.any(HTMLElement),
jasmine.objectContaining({mapTypeId: 'terrain'}));

fixture.componentInstance.mapTypeId = 'roadmap' as unknown as google.maps.MapTypeId;
fixture.detectChanges();

expect(mapSpy.setMapTypeId).toHaveBeenCalledWith('roadmap');
});
});

@Component({
Expand All @@ -289,6 +311,7 @@ describe('GoogleMap', () => {
[center]="center"
[zoom]="zoom"
[options]="options"
[mapTypeId]="mapTypeId"
(mapClick)="handleClick($event)"
(centerChanged)="handleCenterChanged()"
(mapRightclick)="handleRightclick($event)">
Expand All @@ -301,6 +324,7 @@ class TestApp {
center?: google.maps.LatLngLiteral;
zoom?: number;
options?: google.maps.MapOptions;
mapTypeId?: google.maps.MapTypeId;

handleClick(event: google.maps.MouseEvent) {}
handleCenterChanged() {}
Expand Down
13 changes: 13 additions & 0 deletions src/google-maps/google-map/google-map.ts
Expand Up @@ -46,6 +46,7 @@ export interface UpdatedGoogleMap extends google.maps.Map {
export const DEFAULT_OPTIONS: google.maps.MapOptions = {
center: {lat: 37.421995, lng: -122.084092},
zoom: 17,
mapTypeId: undefined
};

/** Arbitrary default height for the map element */
Expand Down Expand Up @@ -79,10 +80,18 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
/** Whether we're currently rendering inside a browser. */
_isBrowser: boolean;

/** Height of the map. */
@Input() height: string | number = DEFAULT_HEIGHT;

/** Width of the map. */
@Input() width: string | number = DEFAULT_WIDTH;

/**
* Type of map that should be rendered. E.g. hybrid map, terrain map etc.
* See: https://developers.google.com/maps/documentation/javascript/reference/map#MapTypeId
*/
@Input() mapTypeId: google.maps.MapTypeId | undefined;

@Input()
set center(center: google.maps.LatLngLiteral|google.maps.LatLng) {
this._center.next(center);
Expand Down Expand Up @@ -249,6 +258,9 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {

ngOnChanges() {
this._setSize();
if (this._googleMap && this.mapTypeId) {
this._googleMap.setMapTypeId(this.mapTypeId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will call setMapTypeId for every change. My preference would be to continue the existing pattern, or failing that, at least include a check that the mapTypeId has changed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct, but from what I can tell, the Google Maps API already knows not to do anything if you're giving it the same map type that it already has.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sounds good

}
}

ngOnInit() {
Expand Down Expand Up @@ -447,6 +459,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
...options,
center: center || options.center,
zoom: zoom !== undefined ? zoom : options.zoom,
mapTypeId: this.mapTypeId
};
return combinedOptions;
}));
Expand Down
2 changes: 1 addition & 1 deletion src/google-maps/testing/fake-google-map-utils.ts
Expand Up @@ -28,7 +28,7 @@ export function createMapSpy(options: google.maps.MapOptions): jasmine.SpyObj<Up
const mapSpy = jasmine.createSpyObj('google.maps.Map', [
'setOptions', 'setCenter', 'setZoom', 'setMap', 'addListener', 'fitBounds', 'panBy', 'panTo',
'panToBounds', 'getBounds', 'getCenter', 'getClickableIcons', 'getHeading', 'getMapTypeId',
'getProjection', 'getStreetView', 'getTilt', 'getZoom'
'getProjection', 'getStreetView', 'getTilt', 'getZoom', 'setMapTypeId'
]);
mapSpy.addListener.and.returnValue({remove: () => {}});
return mapSpy;
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/google-maps/google-maps.d.ts
Expand Up @@ -18,6 +18,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
mapMouseout: Observable<google.maps.MouseEvent>;
mapMouseover: Observable<google.maps.MouseEvent>;
mapRightclick: Observable<google.maps.MouseEvent>;
mapTypeId: google.maps.MapTypeId | undefined;
get mapTypes(): google.maps.MapTypeRegistry;
maptypeidChanged: Observable<void>;
set options(options: google.maps.MapOptions);
Expand Down Expand Up @@ -46,7 +47,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
panBy(x: number, y: number): void;
panTo(latLng: google.maps.LatLng | google.maps.LatLngLiteral): void;
panToBounds(latLngBounds: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral, padding?: number | google.maps.Padding): void;
static ɵcmp: i0.ɵɵComponentDefWithMeta<GoogleMap, "google-map", never, { "height": "height"; "width": "width"; "center": "center"; "zoom": "zoom"; "options": "options"; }, { "boundsChanged": "boundsChanged"; "centerChanged": "centerChanged"; "mapClick": "mapClick"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "mapDragstart": "mapDragstart"; "headingChanged": "headingChanged"; "idle": "idle"; "maptypeidChanged": "maptypeidChanged"; "mapMousemove": "mapMousemove"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "projectionChanged": "projectionChanged"; "mapRightclick": "mapRightclick"; "tilesloaded": "tilesloaded"; "tiltChanged": "tiltChanged"; "zoomChanged": "zoomChanged"; }, never>;
static ɵcmp: i0.ɵɵComponentDefWithMeta<GoogleMap, "google-map", never, { "height": "height"; "width": "width"; "mapTypeId": "mapTypeId"; "center": "center"; "zoom": "zoom"; "options": "options"; }, { "boundsChanged": "boundsChanged"; "centerChanged": "centerChanged"; "mapClick": "mapClick"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "mapDragstart": "mapDragstart"; "headingChanged": "headingChanged"; "idle": "idle"; "maptypeidChanged": "maptypeidChanged"; "mapMousemove": "mapMousemove"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "projectionChanged": "projectionChanged"; "mapRightclick": "mapRightclick"; "tilesloaded": "tilesloaded"; "tiltChanged": "tiltChanged"; "zoomChanged": "zoomChanged"; }, never>;
static ɵfac: i0.ɵɵFactoryDef<GoogleMap>;
}

Expand Down