UNPKG

3.33 kBPlain TextView Raw
1import {Injectable, NgZone} from '@angular/core';
2
3import {Observable} from 'rxjs/Observable';
4import {Observer} from 'rxjs/Observer';
5
6import {SebmGoogleMapCircle} from '../../directives/google-map-circle';
7import {GoogleMapsAPIWrapper} from '../google-maps-api-wrapper';
8import * as mapTypes from '../google-maps-types';
9
10@Injectable()
11export class CircleManager {
12 private _circles: Map<SebmGoogleMapCircle, Promise<mapTypes.Circle>> =
13 new Map<SebmGoogleMapCircle, Promise<mapTypes.Circle>>();
14
15 constructor(private _apiWrapper: GoogleMapsAPIWrapper, private _zone: NgZone) {}
16
17 addCircle(circle: SebmGoogleMapCircle) {
18 this._circles.set(circle, this._apiWrapper.createCircle({
19 center: {lat: circle.latitude, lng: circle.longitude},
20 clickable: circle.clickable,
21 draggable: circle.draggable,
22 editable: circle.editable,
23 fillColor: circle.fillColor,
24 fillOpacity: circle.fillOpacity,
25 radius: circle.radius,
26 strokeColor: circle.strokeColor,
27 strokeOpacity: circle.strokeOpacity,
28 strokePosition: circle.strokePosition,
29 strokeWeight: circle.strokeWeight,
30 visible: circle.visible,
31 zIndex: circle.zIndex
32 }));
33 };
34
35 /**
36 * Removes the given circle from the map.
37 */
38 removeCircle(circle: SebmGoogleMapCircle): Promise<void> {
39 return this._circles.get(circle).then((c) => {
40 c.setMap(null);
41 this._circles.delete(circle);
42 });
43 }
44
45 setOptions(circle: SebmGoogleMapCircle, options: mapTypes.CircleOptions): Promise<void> {
46 return this._circles.get(circle).then((c) => c.setOptions(options));
47 };
48
49 getBounds(circle: SebmGoogleMapCircle): Promise<mapTypes.LatLngBounds> {
50 return this._circles.get(circle).then((c) => c.getBounds());
51 };
52
53 getCenter(circle: SebmGoogleMapCircle): Promise<mapTypes.LatLng> {
54 return this._circles.get(circle).then((c) => c.getCenter());
55 };
56
57 getRadius(circle: SebmGoogleMapCircle): Promise<number> {
58 return this._circles.get(circle).then((c) => c.getRadius());
59 }
60
61 setCenter(circle: SebmGoogleMapCircle): Promise<void> {
62 return this._circles.get(circle).then(
63 (c) => { return c.setCenter({lat: circle.latitude, lng: circle.longitude}); });
64 };
65
66 setEditable(circle: SebmGoogleMapCircle): Promise<void> {
67 return this._circles.get(circle).then((c) => { return c.setEditable(circle.editable); });
68 };
69
70 setDraggable(circle: SebmGoogleMapCircle): Promise<void> {
71 return this._circles.get(circle).then((c) => { return c.setDraggable(circle.draggable); });
72 };
73
74 setVisible(circle: SebmGoogleMapCircle): Promise<void> {
75 return this._circles.get(circle).then((c) => { return c.setVisible(circle.visible); });
76 };
77
78 setRadius(circle: SebmGoogleMapCircle): Promise<void> {
79 return this._circles.get(circle).then((c) => { return c.setRadius(circle.radius); });
80 };
81
82 createEventObservable<T>(eventName: string, circle: SebmGoogleMapCircle): Observable<T> {
83 return Observable.create((observer: Observer<T>) => {
84 let listener: mapTypes.MapsEventListener = null;
85 this._circles.get(circle).then((c) => {
86 listener = c.addListener(eventName, (e: T) => this._zone.run(() => observer.next(e)));
87 });
88
89 return () => {
90 if (listener !== null) {
91 listener.remove();
92 }
93 };
94 });
95 }
96}