@@ -26,7 +26,6 @@ import { combineLatest, Observable, Subscription } from 'rxjs';
2626import { distinctUntilChanged , take } from 'rxjs/operators' ;
2727import { selectMapSelectionToolsVisible , State } from 'src/app/reducers' ;
2828import { ActiveFilter , FilterOption } from 'src/app/shared/models' ;
29- import { boundsToTurfPolygon , mapsPolygonToTurfPolygon } from 'src/app/util' ;
3029import {
3130 MapActions ,
3231 PreSolveShipmentActions ,
@@ -47,10 +46,10 @@ import * as fromUI from '../../selectors/ui.selectors';
4746import { selectPage } from '../../selectors/ui.selectors' ;
4847import TravelSimulatorSelectors from '../../selectors/travel-simulator.selectors' ;
4948import {
50- MATERIAL_COLORS ,
5149 VehicleInfoWindowService ,
5250 VisitRequestInfoWindowService ,
5351 MultiselectInfoWindowService ,
52+ TerraDrawService ,
5453} from '../../services' ;
5554import { DepotLayer } from '../../services/depot-layer.service' ;
5655import { MapService } from '../../services/map.service' ;
@@ -60,6 +59,7 @@ import { PreSolveVehicleLayer } from '../../services/pre-solve-vehicle-layer.ser
6059import { PreSolveVisitRequestLayer } from '../../services/pre-solve-visit-request-layer.service' ;
6160import { RouteLayer } from '../../services/route-layer.service' ;
6261import { MapLayer , MapLayerId } from '../../models/map' ;
62+ import { MapInitializeEvent } from '../map-wrapper/map-wrapper.component' ;
6363
6464@Component ( {
6565 selector : 'app-map' ,
@@ -80,8 +80,8 @@ export class MapComponent implements OnInit, OnDestroy {
8080 }
8181
8282 private readonly subscriptions : Subscription [ ] = [ ] ;
83- private drawingManager : google . maps . drawing . DrawingManager ;
8483 private map : google . maps . Map ;
84+ private mapElement : HTMLElement ;
8585 private page : Page ;
8686
8787 constructor (
@@ -96,7 +96,8 @@ export class MapComponent implements OnInit, OnDestroy {
9696 private preSolveVisitRequestLayer : PreSolveVisitRequestLayer ,
9797 public vehicleInfoWindowService : VehicleInfoWindowService ,
9898 public visitRequestInfoWindowService : VisitRequestInfoWindowService ,
99- public multiselectInfoWindowService : MultiselectInfoWindowService
99+ public multiselectInfoWindowService : MultiselectInfoWindowService ,
100+ private terraDrawService : TerraDrawService
100101 ) { }
101102
102103 ngOnInit ( ) : void {
@@ -160,6 +161,7 @@ export class MapComponent implements OnInit, OnDestroy {
160161
161162 ngOnDestroy ( ) : void {
162163 this . subscriptions . splice ( 0 ) . forEach ( ( subscription ) => subscription . unsubscribe ( ) ) ;
164+ this . terraDrawService . destroy ( ) ;
163165 }
164166
165167 private createFilter ( option : FilterOption ) : ActiveFilter {
@@ -195,16 +197,7 @@ export class MapComponent implements OnInit, OnDestroy {
195197 }
196198
197199 onToggleSelectMapItems ( mode : SelectionMode ) : void {
198- if ( mode === SelectionMode . Off ) {
199- this . drawingManager . setMap ( null ) ;
200- } else {
201- this . drawingManager . setMap ( this . map ) ;
202- this . drawingManager . setDrawingMode (
203- mode === SelectionMode . Bbox
204- ? google . maps . drawing . OverlayType . RECTANGLE
205- : google . maps . drawing . OverlayType . POLYGON
206- ) ;
207- }
200+ this . terraDrawService . setMode ( mode ) ;
208201 }
209202
210203 onTypeChange ( satellite : boolean ) : void {
@@ -215,44 +208,31 @@ export class MapComponent implements OnInit, OnDestroy {
215208 this . mapService . zoomToHome ( ) ;
216209 }
217210
218- onMapInitialize ( map : google . maps . Map ) : void {
219- this . map = map ;
220- this . drawingManager = this . createDrawingTools ( ) ;
211+ onMapInitialize ( event : MapInitializeEvent ) : void {
212+ this . map = event . map ;
213+ this . mapElement = event . element ;
214+ this . initializeTerraDraw ( ) ;
221215 }
222216
223- createDrawingTools ( ) : google . maps . drawing . DrawingManager {
224- const drawingManager = new google . maps . drawing . DrawingManager ( {
225- drawingControl : false ,
226- drawingMode : google . maps . drawing . OverlayType . RECTANGLE ,
227- rectangleOptions : {
228- fillOpacity : 0.0 ,
229- strokeColor : MATERIAL_COLORS . Red . hex ,
230- } ,
231- polygonOptions : {
232- fillOpacity : 0.0 ,
233- strokeColor : MATERIAL_COLORS . Red . hex ,
234- } ,
235- } ) ;
217+ private initializeTerraDraw ( ) : void {
218+ if ( ! this . mapElement ) {
219+ console . error ( 'Map element not found for TerraDraw initialization' ) ;
220+ return ;
221+ }
236222
237- google . maps . event . addListener ( drawingManager , 'overlaycomplete' , ( event ) => {
238- let polygon ;
239- if ( event . type === 'rectangle' ) {
240- const bounds = event . overlay . getBounds ( ) ;
241- polygon = boundsToTurfPolygon ( bounds ) ;
242- } else {
243- polygon = mapsPolygonToTurfPolygon ( event . overlay ) ;
244- }
245- event . overlay . setMap ( null ) ;
223+ this . terraDrawService . initialize ( this . map , this . mapElement ) ;
246224
247- if ( this . page === Page . Shipments ) {
248- this . store . dispatch ( MapActions . selectPreSolveShipmentMapItems ( { polygon } ) ) ;
249- } else if ( this . page === Page . Vehicles ) {
250- this . store . dispatch ( MapActions . selectPreSolveVehicleMapItems ( { polygon } ) ) ;
251- } else if ( this . page === Page . RoutesChart ) {
252- this . store . dispatch ( MapActions . selectPostSolveMapItems ( { polygon } ) ) ;
253- }
254- } ) ;
255- return drawingManager ;
225+ this . subscriptions . push (
226+ this . terraDrawService . onFeatureComplete$ . subscribe ( ( polygon ) => {
227+ if ( this . page === Page . Shipments ) {
228+ this . store . dispatch ( MapActions . selectPreSolveShipmentMapItems ( { polygon } ) ) ;
229+ } else if ( this . page === Page . Vehicles ) {
230+ this . store . dispatch ( MapActions . selectPreSolveVehicleMapItems ( { polygon } ) ) ;
231+ } else if ( this . page === Page . RoutesChart ) {
232+ this . store . dispatch ( MapActions . selectPostSolveMapItems ( { polygon } ) ) ;
233+ }
234+ } )
235+ ) ;
256236 }
257237
258238 isPreSolve ( ) : boolean {
0 commit comments