Skip to content

Commit

Permalink
feat(SebmGoogleMap): support panning
Browse files Browse the repository at this point in the history
You can now use the panning feature of Google Maps when use
`usePanning` binding:

```
<sebm-google-map <sebm-google-map [latitude]="lat" [longitude]="lng"
[zoom]="9" [usePanning]="true">
</sebm-google-map>
```

When latitude/latitude values change and usePanning is true,
the map uses the `panTo()` method of Google Maps.

BREAKING CHANGES:

The latitude, longitude and zoom inputs of <sebm-google-map> must be of type number now.
Strings are not supported any more.

Example:

Old (now unsuported way):
```
<sebm-google-map latitude="33" longitude="22" zoom="8">...
```

New:
```
<sebm-google-map [latitude]="33" [longitude]="22" [zoom]="8">...
```

Closes #412
Closes #416
  • Loading branch information
sebholstein committed Jun 11, 2016
1 parent 7e09969 commit 760f410
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
92 changes: 42 additions & 50 deletions src/core/directives/google-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {MarkerManager} from '../services/marker-manager';
inputs: [
'longitude', 'latitude', 'zoom', 'disableDoubleClickZoom', 'disableDefaultUI', 'scrollwheel',
'backgroundColor', 'draggableCursor', 'draggingCursor', 'keyboardShortcuts', 'zoomControl',
'styles'
'styles', 'usePanning'
],
outputs: ['mapClick', 'mapRightClick', 'mapDblClick', 'centerChange'],
host: {'[class.sebm-google-map-container]': 'true'},
Expand All @@ -60,9 +60,21 @@ import {MarkerManager} from '../services/marker-manager';
})
export class SebmGoogleMap implements OnChanges,
OnInit {
private _longitude: number = 0;
private _latitude: number = 0;
private _zoom: number = 8;
/**
* The longitude that defines the center of the map.
*/
longitude: number = 0;

/**
* The latitude that defines the center of the map.
*/
latitude: number = 0;

/**
* The zoom level of the map. The default zoom level is 8.
*/
zoom: number = 8;

/**
* Enables/disables zoom and center on double click. Enabled by default.
*/
Expand Down Expand Up @@ -118,6 +130,13 @@ export class SebmGoogleMap implements OnChanges,
*/
styles: MapTypeStyle[] = [];

/**
* When true and the latitude and/or longitude values changes, the Google Maps panTo method is
* used to
* center the map. See: https://developers.google.com/maps/documentation/javascript/reference#Map
*/
usePanning: boolean = false;

/**
* Map option attributes that can change over time
*/
Expand Down Expand Up @@ -159,8 +178,8 @@ export class SebmGoogleMap implements OnChanges,

private _initMapInstance(el: HTMLElement) {
this._mapsWrapper.createMap(el, {
center: {lat: this._latitude, lng: this._longitude},
zoom: this._zoom,
center: {lat: this.latitude, lng: this.longitude},
zoom: this.zoom,
disableDefaultUI: this.disableDefaultUI,
backgroundColor: this.backgroundColor,
draggableCursor: this.draggableCursor,
Expand All @@ -177,6 +196,9 @@ export class SebmGoogleMap implements OnChanges,
/* @internal */
ngOnChanges(changes: {[propName: string]: SimpleChange}) {
this._updateMapOptionsChanges(changes);
if (changes['latitude'] != null || changes['longitude']) {
this._updateCenter();
}
}

private _updateMapOptionsChanges(changes: {[propName: string]: SimpleChange}) {
Expand All @@ -201,64 +223,34 @@ export class SebmGoogleMap implements OnChanges,
});
}

/**
* Sets the zoom level of the map. The default value is `8`.
*/
set zoom(value: number|string) {
this._zoom = this._convertToDecimal(value, 8);
if (typeof this._zoom === 'number') {
this._mapsWrapper.setZoom(this._zoom);
}
}

/**
* The longitude that sets the center of the map.
*/
set longitude(value: number|string) {
this._longitude = this._convertToDecimal(value);
this._updateCenter();
}

/**
* The latitude that sets the center of the map.
*/
set latitude(value: number|string) {
this._latitude = this._convertToDecimal(value);
this._updateCenter();
}

private _convertToDecimal(value: string|number, defaultValue: number = null): number {
if (typeof value === 'string') {
return parseFloat(value);
} else if (typeof value === 'number') {
return <number>value;
}
return defaultValue;
}

private _updateCenter() {
if (typeof this._latitude !== 'number' || typeof this._longitude !== 'number') {
if (typeof this.latitude !== 'number' || typeof this.longitude !== 'number') {
return;
}
this._mapsWrapper.setCenter({
lat: this._latitude,
lng: this._longitude,
});
let newCenter = {
lat: this.latitude,
lng: this.longitude,
};
if (this.usePanning) {
this._mapsWrapper.panTo(newCenter);
} else {
this._mapsWrapper.setCenter(newCenter);
}
}

private _handleMapCenterChange() {
this._mapsWrapper.subscribeToMapEvent<void>('center_changed').subscribe(() => {
this._mapsWrapper.getCenter().then((center: LatLng) => {
this._latitude = center.lat();
this._longitude = center.lng();
this.centerChange.emit(<LatLngLiteral>{lat: this._latitude, lng: this._longitude});
this.latitude = center.lat();
this.longitude = center.lng();
this.centerChange.emit(<LatLngLiteral>{lat: this.latitude, lng: this.longitude});
});
});
}

private _handleMapZoomChange() {
this._mapsWrapper.subscribeToMapEvent<void>('zoom_changed').subscribe(() => {
this._mapsWrapper.getZoom().then((z: number) => this._zoom = z);
this._mapsWrapper.getZoom().then((z: number) => this.zoom = z);
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/services/google-maps-api-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class GoogleMapsAPIWrapper {
return this._map.then((map: mapTypes.GoogleMap) => map.getCenter());
}

panTo(latLng: mapTypes.LatLng|mapTypes.LatLngLiteral): Promise<void> {
return this._map.then((map) => map.panTo(latLng));
}

/**
* Returns the native Google Maps Map instance. Be careful when using this instance directly.
*/
Expand Down

0 comments on commit 760f410

Please sign in to comment.