Skip to content

Commit

Permalink
feat(SebmGoogleMapMarker): support click event
Browse files Browse the repository at this point in the history
Now, it’s possible to get notified when a marker gets clicked:

<sebm-google-map-marker [latitude]="m.lat" [longitude]="m.lng"
(markerClick)="markerClicked()"></sebm-google-map-marker>
  • Loading branch information
sebholstein committed Dec 12, 2015
1 parent 661641a commit 2926de7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/components/google_map_marker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import {Directive, Input, SimpleChange, OnDestroy, OnChanges} from 'angular2/angular2';
import {
Directive,
Input,
SimpleChange,
OnDestroy,
OnChanges,
EventEmitter,
Output
} from 'angular2/angular2';
import {MarkerManager} from '../services/marker_manager';

let markerId = 0;
Expand All @@ -9,6 +17,7 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges {
@Input() longitude: number;
@Input() title: string;
@Input() label: string;
@Output() markerClick: EventEmitter<void> = new EventEmitter<void>();

private _markerAddedToManger: boolean = false;
private _id: string;
Expand All @@ -19,6 +28,8 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges {
if (!this._markerAddedToManger && this.latitude && this.longitude) {
this._markerManager.addMarker(this);
this._markerAddedToManger = true;
this._markerManager.createClickObserable(this)
.subscribe(() => { this.markerClick.next(null); });
return;
}
if (changes['latitude'] || changes['logitude']) {
Expand Down
1 change: 1 addition & 0 deletions src/custom_typings/google_maps.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ declare module google.maps {
setTitle(title: string): void;
setLabel(label: string | MarkerLabel): void;
getLabel(): MarkerLabel;
addListener(eventType: string, fn: Function): void;
}

export interface MarkerOptions {
Expand Down
10 changes: 9 additions & 1 deletion src/services/marker_manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Injectable} from 'angular2/angular2';
import {Injectable, Observable} from 'angular2/angular2';
import {Observer} from 'rxjs/Observer';
import {SebmGoogleMapMarker} from '../components/google_map_marker';
import {GoogleMapsAPIWrapper} from './google_maps_api_wrapper';

Expand Down Expand Up @@ -37,4 +38,11 @@ export class MarkerManager {
{position: {lat: marker.latitude, lng: marker.longitude}, label: marker.label});
this._markers.set(marker, markerPromise);
}

createClickObserable(marker: SebmGoogleMapMarker): Observable<void> {
return Observable.create((observer: Observer<void>) => {
this._markers.get(marker).then(
(m: google.maps.Marker) => { m.addListener('click', () => { observer.next(null); }); });
});
}
}

0 comments on commit 2926de7

Please sign in to comment.