Skip to content

Commit

Permalink
feat(SebmGoogleMap): support fitBounds
Browse files Browse the repository at this point in the history
<sebm-google-map [fitBounds]="myBounds"></sebm-google-map>

Closes #283
Closes #492
  • Loading branch information
sebholstein committed Jul 9, 2016
1 parent c20d005 commit e913394
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/core/directives/google-map-circle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Directive, EventEmitter, OnChanges, OnDestroy, OnInit, SimpleChange} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';

import {MouseEvent} from '../events';
import {MouseEvent} from '../map-types';
import {LatLng, LatLngBounds, LatLngLiteral} from '../services/google-maps-types';
import {MouseEvent as MapMouseEvent} from '../services/google-maps-types';
import {CircleManager} from '../services/managers/circle-manager';
Expand Down
4 changes: 2 additions & 2 deletions src/core/directives/google-map-marker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AfterContentInit, ContentChild, Directive, EventEmitter, OnChanges, OnDestroy, SimpleChange} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';

import {MouseEvent} from '../events';
import {MouseEvent} from '../map-types';
import * as mapTypes from '../services/google-maps-types';
import {MarkerManager} from '../services/managers/marker-manager';

Expand Down Expand Up @@ -38,7 +38,7 @@ let markerId = 0;
selector: 'sebm-google-map-marker',
inputs: [
'latitude', 'longitude', 'title', 'label', 'draggable: markerDraggable', 'iconUrl',
'openInfoWindow'
'openInfoWindow', 'fitBounds'
],
outputs: ['markerClick', 'dragEnd']
})
Expand Down
44 changes: 35 additions & 9 deletions src/core/directives/google-map.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Component, ElementRef, EventEmitter, OnChanges, OnInit, SimpleChange} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';

import {MouseEvent} from '../events';
import {MouseEvent} from '../map-types';
import {GoogleMapsAPIWrapper} from '../services/google-maps-api-wrapper';
import {LatLng, LatLngLiteral} from '../services/google-maps-types';
import {LatLngBounds, MapTypeStyle} from '../services/google-maps-types';
import {LatLngBounds, LatLngBoundsLiteral, MapTypeStyle} from '../services/google-maps-types';
import {CircleManager} from '../services/managers/circle-manager';
import {InfoWindowManager} from '../services/managers/info-window-manager';
import {MarkerManager} from '../services/managers/marker-manager';
Expand Down Expand Up @@ -40,7 +40,7 @@ import {MarkerManager} from '../services/managers/marker-manager';
inputs: [
'longitude', 'latitude', 'zoom', 'disableDoubleClickZoom', 'disableDefaultUI', 'scrollwheel',
'backgroundColor', 'draggableCursor', 'draggingCursor', 'keyboardShortcuts', 'zoomControl',
'styles', 'usePanning', 'streetViewControl'
'styles', 'usePanning', 'streetViewControl', 'fitBounds'
],
outputs: ['mapClick', 'mapRightClick', 'mapDblClick', 'centerChange', 'idle', 'boundsChange'],
host: {'[class.sebm-google-map-container]': 'true'},
Expand Down Expand Up @@ -145,6 +145,11 @@ export class SebmGoogleMap implements OnChanges, OnInit {
*/
streetViewControl: boolean = true;

/**
* Sets the viewport to contain the given bounds.
*/
fitBounds: LatLngBoundsLiteral|LatLngBounds = null;

/**
* Map option attributes that can change over time
*/
Expand Down Expand Up @@ -199,7 +204,7 @@ export class SebmGoogleMap implements OnChanges, OnInit {

private _initMapInstance(el: HTMLElement) {
this._mapsWrapper.createMap(el, {
center: {lat: this.latitude, lng: this.longitude},
center: {lat: this.latitude || 0, lng: this.longitude || 0},
zoom: this.zoom,
disableDefaultUI: this.disableDefaultUI,
backgroundColor: this.backgroundColor,
Expand All @@ -210,6 +215,8 @@ export class SebmGoogleMap implements OnChanges, OnInit {
styles: this.styles,
streetViewControl: this.streetViewControl
});

// register event listeners
this._handleMapCenterChange();
this._handleMapZoomChange();
this._handleMapMouseEvents();
Expand All @@ -226,9 +233,7 @@ export class SebmGoogleMap implements OnChanges, OnInit {
/* @internal */
ngOnChanges(changes: {[propName: string]: SimpleChange}) {
this._updateMapOptionsChanges(changes);
if (changes['latitude'] != null || changes['longitude'] != null) {
this._updateCenter();
}
this._updatePosition(changes);
}

private _updateMapOptionsChanges(changes: {[propName: string]: SimpleChange}) {
Expand All @@ -253,7 +258,19 @@ export class SebmGoogleMap implements OnChanges, OnInit {
});
}

private _updateCenter() {
private _updatePosition(changes: {[propName: string]: SimpleChange}) {
if (changes['latitude'] == null && changes['longitude'] == null &&
changes['fitBounds'] == null) {
// no position update needed
return;
}

// we prefer fitBounds in changes
if (changes['fitBounds'] && this.fitBounds != null) {
this._fitBounds();
return;
}

if (typeof this.latitude !== 'number' || typeof this.longitude !== 'number') {
return;
}
Expand All @@ -268,6 +285,14 @@ export class SebmGoogleMap implements OnChanges, OnInit {
}
}

private _fitBounds() {
if (this.usePanning) {
this._mapsWrapper.panToBounds(this.fitBounds);
return;
}
this._mapsWrapper.fitBounds(this.fitBounds);
}

private _handleMapCenterChange() {
const s = this._mapsWrapper.subscribeToMapEvent<void>('center_changed').subscribe(() => {
this._mapsWrapper.getCenter().then((center: LatLng) => {
Expand All @@ -281,7 +306,8 @@ export class SebmGoogleMap implements OnChanges, OnInit {

private _handleBoundsChange() {
const s = this._mapsWrapper.subscribeToMapEvent<void>('bounds_changed').subscribe(() => {
this._mapsWrapper.getBounds().then((bounds: LatLngBounds) => this.boundsChange.emit(bounds));
this._mapsWrapper.getBounds().then(
(bounds: LatLngBounds) => { this.boundsChange.emit(bounds); });
});
this._observableSubscriptions.push(s);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {BROWSER_GLOBALS_PROVIDERS} from './utils/browser-globals';
// main modules
export * from './directives';
export * from './services';
export * from './events';
export * from './map-types';

// Google Maps types
export {LatLngBounds, LatLng, LatLngLiteral, MapTypeStyle} from './services/google-maps-types';
Expand Down
2 changes: 1 addition & 1 deletion src/core/events.ts → src/core/map-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {LatLngLiteral} from './services/google-maps-types';

// exported map types
export {LatLngLiteral} from './services/google-maps-types';
export {LatLngBounds, LatLngBoundsLiteral, LatLngLiteral} from './services/google-maps-types';

/**
* MouseEvent gets emitted when the user triggers mouse events on the map.
Expand Down
8 changes: 8 additions & 0 deletions src/core/services/google-maps-api-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export class GoogleMapsAPIWrapper {
return this._map.then((map) => map.panTo(latLng));
}

fitBounds(latLng: mapTypes.LatLngBounds|mapTypes.LatLngBoundsLiteral): Promise<void> {
return this._map.then((map) => map.fitBounds(latLng));
}

panToBounds(latLng: mapTypes.LatLngBounds|mapTypes.LatLngBoundsLiteral): Promise<void> {
return this._map.then((map) => map.panToBounds(latLng));
}

/**
* Returns the native Google Maps Map instance. Be careful when using this instance directly.
*/
Expand Down
10 changes: 4 additions & 6 deletions src/core/services/google-maps-types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
export var google: any;

export interface GoogleMap {
export interface GoogleMap extends MVCObject {
constructor(el: HTMLElement, opts?: MapOptions): void;
panTo(latLng: LatLng|LatLngLiteral): void;
setZoom(zoom: number): void;
addListener(eventName: string, fn: Function): void;
getCenter(): LatLng;
setCenter(latLng: LatLng|LatLngLiteral): void;
getBounds(): LatLngBounds;
getZoom(): number;
setOptions(options: MapOptions): void;
panToBounds(latLngBounds: LatLngBounds|LatLngBoundsLiteral): void;
fitBounds(bounds: LatLngBounds|LatLngBoundsLiteral): void;
}

export interface LatLng {
Expand Down Expand Up @@ -165,10 +166,7 @@ export interface InfoWindow {
setZIndex(zIndex: number): void;
}

export interface MVCObject {
constructor(): void;
addListener(eventName: string, handler: Function): MapsEventListener;
}
export interface MVCObject { addListener(eventName: string, handler: Function): MapsEventListener; }

export interface MapsEventListener { remove(): void; }

Expand Down

0 comments on commit e913394

Please sign in to comment.