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(SebmGoogleMapMarker): support dragend event #149

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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