Skip to content

Commit

Permalink
feat(SebmGoogleMapMarker): support dragend event
Browse files Browse the repository at this point in the history
closes #71
closes #149
  • Loading branch information
sebholstein committed Feb 21, 2016
1 parent 978e881 commit 8f60c54
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
22 changes: 19 additions & 3 deletions src/directives/google-map-marker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Directive, SimpleChange, OnDestroy, OnChanges, EventEmitter} from 'angular2/core';
import {MarkerManager} from '../services/marker-manager';
import {MouseEvent} from '../events';
import * as mapTypes from '../services/google-maps-types';

let markerId = 0;

Expand Down Expand Up @@ -31,7 +33,7 @@ let markerId = 0;
@Directive({
selector: 'sebm-google-map-marker',
inputs: ['latitude', 'longitude', 'title', 'label', 'draggable: markerDraggable'],
outputs: ['markerClick']
outputs: ['markerClick', 'dragEnd']
})
export class SebmGoogleMapMarker implements OnDestroy,
OnChanges {
Expand Down Expand Up @@ -65,6 +67,11 @@ export class SebmGoogleMapMarker implements OnDestroy,
*/
markerClick: EventEmitter<void> = new EventEmitter<void>();

/**
* This event is fired when the user stops dragging the marker.
*/
dragEnd: EventEmitter<MouseEvent> = new EventEmitter<MouseEvent>();

private _markerAddedToManger: boolean = false;
private _id: string;

Expand All @@ -78,8 +85,7 @@ export class SebmGoogleMapMarker implements OnDestroy,
if (!this._markerAddedToManger) {
this._markerManager.addMarker(this);
this._markerAddedToManger = true;
this._markerManager.createClickObserable(this).subscribe(
() => { this.markerClick.next(null); });
this._addEventListeners();
return;
}
if (changes['latitude'] || changes['logitude']) {
Expand All @@ -96,6 +102,16 @@ export class SebmGoogleMapMarker implements OnDestroy,
}
}

private _addEventListeners() {
this._markerManager.createEventObservable('click', this).subscribe(() => {
this.markerClick.next(null);
});
this._markerManager.createEventObservable<mapTypes.MouseEvent>('dragend', this)
.subscribe((e: mapTypes.MouseEvent) => {
this.dragEnd.next({coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}});
});
}

/** @internal */
id(): string { return this._id; }

Expand Down
2 changes: 2 additions & 0 deletions src/services/google-maps-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface LatLngLiteral {
lng: number;
}

export interface MouseEvent { latLng: LatLng; }

export interface MapOptions {
center?: LatLng | LatLngLiteral;
zoom?: number;
Expand Down
6 changes: 3 additions & 3 deletions src/services/marker-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export class MarkerManager {
this._markers.set(marker, markerPromise);
}

createClickObserable(marker: SebmGoogleMapMarker): Observable<void> {
return Observable.create((observer: Observer<void>) => {
createEventObservable<T>(eventName: string, marker: SebmGoogleMapMarker): Observable<T> {
return Observable.create((observer: Observer<T>) => {
this._markers.get(marker).then((m: Marker) => {
m.addListener('click', () => this._zone.run(() => observer.next(null)));
m.addListener(eventName, (e: T) => this._zone.run(() => observer.next(e)));
});
});
}
Expand Down

0 comments on commit 8f60c54

Please sign in to comment.