UNPKG

2.65 kBPlain TextView Raw
1import {Injectable, NgZone} from '@angular/core';
2import {Observable} from 'rxjs/Observable';
3import {Observer} from 'rxjs/Observer';
4
5import {SebmGoogleMapPolyline} from '../../directives/google-map-polyline';
6import {SebmGoogleMapPolylinePoint} from '../../directives/google-map-polyline-point';
7import {GoogleMapsAPIWrapper} from '../google-maps-api-wrapper';
8import {LatLngLiteral, Polyline} from '../google-maps-types';
9
10@Injectable()
11export class PolylineManager {
12 private _polylines: Map<SebmGoogleMapPolyline, Promise<Polyline>> =
13 new Map<SebmGoogleMapPolyline, Promise<Polyline>>();
14
15 constructor(private _mapsWrapper: GoogleMapsAPIWrapper, private _zone: NgZone) {}
16
17 private static _convertPoints(line: SebmGoogleMapPolyline): Array<LatLngLiteral> {
18 const path = line._getPoints().map((point: SebmGoogleMapPolylinePoint) => {
19 return <LatLngLiteral>{lat: point.latitude, lng: point.longitude};
20 });
21 return path;
22 }
23
24 addPolyline(line: SebmGoogleMapPolyline) {
25 const path = PolylineManager._convertPoints(line);
26 const polylinePromise = this._mapsWrapper.createPolyline({
27 clickable: line.clickable,
28 draggable: line.draggable,
29 editable: line.editable,
30 geodesic: line.geodesic,
31 strokeColor: line.strokeColor,
32 strokeOpacity: line.strokeOpacity,
33 strokeWeight: line.strokeWeight,
34 visible: line.visible,
35 zIndex: line.zIndex,
36 path: path
37 });
38 this._polylines.set(line, polylinePromise);
39 }
40
41 updatePolylinePoints(line: SebmGoogleMapPolyline): Promise<void> {
42 const path = PolylineManager._convertPoints(line);
43 const m = this._polylines.get(line);
44 if (m == null) {
45 return Promise.resolve();
46 }
47 return m.then((l: Polyline) => { return this._zone.run(() => { l.setPath(path); }); });
48 }
49
50 setPolylineOptions(line: SebmGoogleMapPolyline, options: {[propName: string]: any}):
51 Promise<void> {
52 return this._polylines.get(line).then((l: Polyline) => { l.setOptions(options); });
53 }
54
55 deletePolyline(line: SebmGoogleMapPolyline): Promise<void> {
56 const m = this._polylines.get(line);
57 if (m == null) {
58 return Promise.resolve();
59 }
60 return m.then((l: Polyline) => {
61 return this._zone.run(() => {
62 l.setMap(null);
63 this._polylines.delete(line);
64 });
65 });
66 }
67
68 createEventObservable<T>(eventName: string, line: SebmGoogleMapPolyline): Observable<T> {
69 return Observable.create((observer: Observer<T>) => {
70 this._polylines.get(line).then((l: Polyline) => {
71 l.addListener(eventName, (e: T) => this._zone.run(() => observer.next(e)));
72 });
73 });
74 }
75}