|
| 1 | +import { Circle, CircleOptions, GoogleMap, MapsEventListener, Marker, MarkerOptions, MVCObject, Polygon, PolygonOptions, Polyline, PolylineOptions, Rectangle, RectangleOptions } from '@agm/core/services/google-maps-types'; |
| 2 | +import { Directive, EventEmitter, Input, isDevMode, NgZone, OnChanges, OnDestroy, Output, SimpleChanges } from '@angular/core'; |
| 3 | +import { fromEventPattern, Observable, Subscription } from 'rxjs'; |
| 4 | +import { DrawingControlOptions, OverlayCompleteEvent, OverlayType } from '../google-drawing-types'; |
| 5 | + |
| 6 | +declare var google: any; |
| 7 | + |
| 8 | +@Directive({ |
| 9 | + selector: 'agm-drawing-manager', |
| 10 | + exportAs: 'agmDrawingManager', |
| 11 | +}) |
| 12 | +export class AgmDrawingManager implements OnChanges, OnDestroy{ |
| 13 | + |
| 14 | + /** |
| 15 | + * The enabled/disabled state of the drawing control. Defaults to `true`. |
| 16 | + * |
| 17 | + * @type {boolean} |
| 18 | + */ |
| 19 | + @Input() drawingControl: boolean; |
| 20 | + |
| 21 | + /** |
| 22 | + * The DrawingManager's drawing mode, which defines the type of overlay to be |
| 23 | + * added on the map. A drawing mode of null means that the user can interact |
| 24 | + * with the map as normal, and clicks do not draw anything. |
| 25 | + */ |
| 26 | + @Input() drawingMode: OverlayType | null; |
| 27 | + |
| 28 | + /** |
| 29 | + * The display options for the drawing control. |
| 30 | + * |
| 31 | + * @type {DrawingControlOptions} |
| 32 | + */ |
| 33 | + @Input() drawingControlOptions: DrawingControlOptions; |
| 34 | + |
| 35 | + /** |
| 36 | + * Options to apply to any new circles created with this DrawingManager. |
| 37 | + * The `center` and `radius` properties are ignored, and the `map` property of a |
| 38 | + * new circle is always set to the DrawingManager's map. |
| 39 | + * |
| 40 | + * @type {CircleOptions} |
| 41 | + */ |
| 42 | + @Input() circleOptions: CircleOptions; |
| 43 | + |
| 44 | + /** |
| 45 | + * Options to apply to any new markers created with this DrawingManager. |
| 46 | + * The `position` property is ignored, and the `map` property of a new marker |
| 47 | + * is always set to the DrawingManager's map. |
| 48 | + * |
| 49 | + * @type {MarkerOptions} |
| 50 | + */ |
| 51 | + @Input() markerOptions: MarkerOptions; |
| 52 | + |
| 53 | + /** |
| 54 | + * Options to apply to any new polygons created with this DrawingManager. |
| 55 | + * The `paths` property is ignored, and the map property of a new polygon is |
| 56 | + * always set to the DrawingManager's map. |
| 57 | + * |
| 58 | + * @type {PolygonOptions} |
| 59 | + */ |
| 60 | + @Input() polygonOptions: PolygonOptions; |
| 61 | + |
| 62 | + /** |
| 63 | + * Options to apply to any new polylines created with this DrawingManager. |
| 64 | + * The `path` property is ignored, and the map property of a new polyline is |
| 65 | + * always set to the DrawingManager's map. |
| 66 | + * |
| 67 | + * @type {PolylineOptions} |
| 68 | + * @memberof AgmDrawingManager |
| 69 | + */ |
| 70 | + @Input() polylineOptions: PolylineOptions; |
| 71 | + |
| 72 | + /** |
| 73 | + * Options to apply to any new rectangles created with this DrawingManager. |
| 74 | + * The `bounds` property is ignored, and the map property of a new rectangle |
| 75 | + * is always set to the DrawingManager's map. |
| 76 | + * |
| 77 | + * @type {RectangleOptions} |
| 78 | + * @memberof AgmDrawingManager |
| 79 | + */ |
| 80 | + @Input() rectangeOptions: RectangleOptions; |
| 81 | + |
| 82 | + /** |
| 83 | + * This event is fired when the user has finished drawing a circle. |
| 84 | + */ |
| 85 | + @Output() circleComplete = new EventEmitter<Circle>(); |
| 86 | + |
| 87 | + /** |
| 88 | + * This event is fired when the user has finished drawing a marker. |
| 89 | + */ |
| 90 | + @Output() markerComplete = new EventEmitter<Marker>(); |
| 91 | + |
| 92 | + /** |
| 93 | + * This event is fired when the user has finished drawing an overlay of any |
| 94 | + * type. |
| 95 | + */ |
| 96 | + @Output() overlayComplete = new EventEmitter<OverlayCompleteEvent>(); |
| 97 | + |
| 98 | + /** |
| 99 | + * This event is fired when the user has finished drawing a polygon. |
| 100 | + */ |
| 101 | + @Output() polygonComplete = new EventEmitter<Polygon>(); |
| 102 | + |
| 103 | + /** |
| 104 | + * This event is fired when the user has finished drawing a polyline. |
| 105 | + */ |
| 106 | + @Output() polylineComplete = new EventEmitter<Polyline>(); |
| 107 | + |
| 108 | + /** |
| 109 | + * This event is fired when the user has finished drawing a rectangle. |
| 110 | + */ |
| 111 | + @Output() rectangleComplete = new EventEmitter<Rectangle>(); |
| 112 | + |
| 113 | + private eventSubscriptions: Subscription[] = []; |
| 114 | + |
| 115 | + private drawingManager: any; |
| 116 | + |
| 117 | + constructor(private _zone: NgZone) { |
| 118 | + } |
| 119 | + |
| 120 | + setMap(map: GoogleMap) { |
| 121 | + if (!google.maps.drawing && isDevMode()) { |
| 122 | + console.error('Cannot use drawing manager if drawing library is not ' + |
| 123 | + 'loaded. To fix, add libraries: [\'drawing\'] to the ' + |
| 124 | + 'lazyMapsAPILoaderConfig you passed to AgmCoreModule.forRoot'); |
| 125 | + return; |
| 126 | + } |
| 127 | + if (map && !this.drawingManager) { |
| 128 | + this.drawingManager = new google.maps.drawing.DrawingManager({ |
| 129 | + map, |
| 130 | + circleOptions: this.circleOptions, |
| 131 | + markerOptions: this.markerOptions, |
| 132 | + polygonOptions: this.polygonOptions, |
| 133 | + polylineOptions: this.polylineOptions, |
| 134 | + rectangeOptions: this.rectangeOptions, |
| 135 | + drawingControl: this.drawingControl, |
| 136 | + drawingControlOptions: this.drawingControlOptions, |
| 137 | + drawingMode: this.drawingMode, |
| 138 | + }); |
| 139 | + this.initEvents(this.drawingManager); |
| 140 | + } else if (!map && this.drawingManager) { |
| 141 | + this.drawingManager.setMap(null); |
| 142 | + } |
| 143 | + // else do nothing |
| 144 | + } |
| 145 | + |
| 146 | + initEvents(drawingManager: any) { |
| 147 | + this.eventSubscriptions.push( |
| 148 | + this.createMvcObservable<Circle>('circlecomplete', drawingManager) |
| 149 | + .subscribe(circle => this._zone.run(() => this.circleComplete.next(circle))) |
| 150 | + ); |
| 151 | + this.eventSubscriptions.push( |
| 152 | + this.createMvcObservable<Marker>('markercomplete', drawingManager) |
| 153 | + .subscribe(marker => this._zone.run(() => this.markerComplete.next(marker))) |
| 154 | + ); |
| 155 | + this.eventSubscriptions.push( |
| 156 | + this.createMvcObservable<Polygon>('polygoncomplete', drawingManager) |
| 157 | + .subscribe(polygon => this._zone.run(() => this.polygonComplete.next(polygon))) |
| 158 | + ); |
| 159 | + this.eventSubscriptions.push( |
| 160 | + this.createMvcObservable<Polyline>('polylinecomplete', drawingManager) |
| 161 | + .subscribe(polyline => this._zone.run(() => this.polylineComplete.next(polyline))) |
| 162 | + ); |
| 163 | + this.eventSubscriptions.push( |
| 164 | + this.createMvcObservable<OverlayCompleteEvent>('overlaycomplete', drawingManager) |
| 165 | + .subscribe(overlayevent => this._zone.run(() => this.overlayComplete.next(overlayevent))) |
| 166 | + ); |
| 167 | + this.eventSubscriptions.push( |
| 168 | + this.createMvcObservable<Rectangle>('rectanglecomplete', drawingManager) |
| 169 | + .subscribe(rectangle => this._zone.run(() => this.rectangleComplete.next(rectangle))) |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + createMvcObservable<E>(eventName: string, mvcObject: MVCObject): Observable<E> { |
| 174 | + return fromEventPattern( |
| 175 | + handler => mvcObject.addListener(eventName, |
| 176 | + (event?: E) => handler.apply(null, [event])), |
| 177 | + (_handler: Function, evListener: MapsEventListener) => evListener.remove() |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + ngOnChanges(changes: SimpleChanges): void { |
| 182 | + if (!this.drawingManager) { |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + const options = Object.entries(changes) |
| 187 | + .map(([prop, change]) => [prop, change.currentValue]) |
| 188 | + .reduce((obj: any, [propName, propValue]) => { |
| 189 | + obj[propName] = propValue; |
| 190 | + return obj; |
| 191 | + }, {}); |
| 192 | + this.drawingManager.setOptions(options); |
| 193 | + } |
| 194 | + |
| 195 | + ngOnDestroy(): void { |
| 196 | + this.eventSubscriptions.forEach(subscription => subscription.unsubscribe()); |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments