@@ -3,8 +3,7 @@ const React = require('react');
33const assign = require ( 'object-assign' ) ;
44var L = require ( 'leaflet' ) ;
55const { slice} = require ( 'lodash' ) ;
6- var CoordinatesUtils = require ( '../../../utils/CoordinatesUtils' ) ;
7-
6+ const { reproject, calculateAzimuth, calculateDistance, transformLineToArcs} = require ( '../../../utils/CoordinatesUtils' ) ;
87require ( 'leaflet-draw' ) ;
98
109class MeasurementSupport extends React . Component {
@@ -35,13 +34,13 @@ class MeasurementSupport extends React.Component {
3534 if ( newProps . measurement . geomType && newProps . measurement . geomType !== this . props . measurement . geomType ) {
3635 this . addDrawInteraction ( newProps ) ;
3736 }
38-
3937 if ( ! newProps . measurement . geomType ) {
4038 this . removeDrawInteraction ( ) ;
4139 }
4240 }
4341
4442 onDrawStart = ( ) => {
43+ this . removeArcLayer ( ) ;
4544 this . drawing = true ;
4645 } ;
4746
@@ -52,12 +51,19 @@ class MeasurementSupport extends React.Component {
5251 // preserve the currently created layer to remove it later on
5352 this . lastLayer = evt . layer ;
5453
54+ let feature = this . lastLayer && this . lastLayer . toGeoJSON ( ) || { } ;
5555 if ( this . props . measurement . geomType === 'Point' ) {
5656 let pos = this . drawControl . _marker . getLatLng ( ) ;
5757 let point = { x : pos . lng , y : pos . lat , srs : 'EPSG:4326' } ;
58- let newMeasureState = assign ( { } , this . props . measurement , { point : point } ) ;
58+ let newMeasureState = assign ( { } , this . props . measurement , { point : point , feature} ) ;
59+ this . props . changeMeasurementState ( newMeasureState ) ;
60+ } else {
61+ let newMeasureState = assign ( { } , this . props . measurement , { feature} ) ;
5962 this . props . changeMeasurementState ( newMeasureState ) ;
6063 }
64+ if ( this . props . measurement . lineMeasureEnabled && this . lastLayer ) {
65+ this . addArcsToMap ( [ feature ] ) ;
66+ }
6167 } ;
6268
6369 render ( ) {
@@ -66,10 +72,36 @@ class MeasurementSupport extends React.Component {
6672 if ( drawingStrings ) {
6773 L . drawLocal = drawingStrings ;
6874 }
69-
7075 return null ;
7176 }
7277
78+ /**
79+ * This method adds arcs converting from a LineString features
80+ */
81+ addArcsToMap = ( features ) => {
82+ this . removeLastLayer ( ) ;
83+ let newFeatures = features . map ( f => {
84+ return assign ( { } , f , {
85+ geometry : assign ( { } , f . geometry , {
86+ coordinates : transformLineToArcs ( f . geometry . coordinates )
87+ } )
88+ } ) ;
89+ } ) ;
90+ this . arcLayer = L . geoJson ( newFeatures , {
91+ style : {
92+ color : '#ffcc33' ,
93+ opacity : 1 ,
94+ weight : 1 ,
95+ fillColor : '#ffffff' ,
96+ fillOpacity : 0.2 ,
97+ clickable : false
98+ }
99+ } ) ;
100+ this . props . map . addLayer ( this . arcLayer ) ;
101+ if ( newFeatures && newFeatures . length > 0 ) {
102+ this . arcLayer . addData ( newFeatures ) ;
103+ }
104+ }
73105 updateMeasurementResults = ( ) => {
74106 if ( ! this . drawing || ! this . drawControl ) {
75107 return ;
@@ -79,10 +111,15 @@ class MeasurementSupport extends React.Component {
79111 let bearing = 0 ;
80112
81113 let currentLatLng = this . drawControl . _currentLatLng ;
82- if ( this . props . measurement . geomType === 'LineString' && this . drawControl . _markers && this . drawControl . _markers . length > 0 ) {
114+ if ( this . props . measurement . geomType === 'LineString' && this . drawControl . _markers && this . drawControl . _markers . length > 1 ) {
83115 // calculate length
84- let previousLatLng = this . drawControl . _markers [ this . drawControl . _markers . length - 1 ] . getLatLng ( ) ;
85- distance = this . drawControl . _measurementRunningTotal + currentLatLng . distanceTo ( previousLatLng ) ;
116+ const reprojectedCoords = this . drawControl . _markers . reduce ( ( p , c ) => {
117+ const { lng, lat} = c . getLatLng ( ) ;
118+ return [ ...p , [ lng , lat ] ] ;
119+ } , [ ] ) ;
120+
121+ distance = calculateDistance ( reprojectedCoords , this . props . measurement . lengthFormula ) ;
122+
86123 } else if ( this . props . measurement . geomType === 'Polygon' && this . drawControl . _poly ) {
87124 // calculate area
88125 let latLngs = [ ...this . drawControl . _poly . getLatLngs ( ) , currentLatLng ] ;
@@ -98,12 +135,12 @@ class MeasurementSupport extends React.Component {
98135 coords2 = [ bearingMarkers [ 1 ] . getLatLng ( ) . lng , bearingMarkers [ 1 ] . getLatLng ( ) . lat ] ;
99136 }
100137 // in order to align the results between leaflet and openlayers the coords are repojected only for leaflet
101- coords1 = CoordinatesUtils . reproject ( coords1 , 'EPSG:4326' , this . props . projection ) ;
102- coords2 = CoordinatesUtils . reproject ( coords2 , 'EPSG:4326' , this . props . projection ) ;
138+ coords1 = reproject ( coords1 , 'EPSG:4326' , this . props . projection ) ;
139+ coords2 = reproject ( coords2 , 'EPSG:4326' , this . props . projection ) ;
103140 // calculate the azimuth as base for bearing information
104- bearing = CoordinatesUtils . calculateAzimuth ( coords1 , coords2 , this . props . projection ) ;
141+ bearing = calculateAzimuth ( coords1 , coords2 , this . props . projection ) ;
105142 }
106-
143+ // let drawn geom stay on the map
107144 let newMeasureState = assign ( { } , this . props . measurement ,
108145 {
109146 point : null , // Point is set in onDraw.created
@@ -199,9 +236,7 @@ class MeasurementSupport extends React.Component {
199236 if ( this . drawControl !== null && this . drawControl !== undefined ) {
200237 this . drawControl . disable ( ) ;
201238 this . drawControl = null ;
202- if ( this . lastLayer ) {
203- this . props . map . removeLayer ( this . lastLayer ) ;
204- }
239+ this . removeLastLayer ( ) ;
205240 this . props . map . off ( 'draw:created' , this . onDrawCreated , this ) ;
206241 this . props . map . off ( 'draw:drawstart' , this . onDrawStart , this ) ;
207242 this . props . map . off ( 'click' , this . mapClickHandler , this ) ;
@@ -210,6 +245,16 @@ class MeasurementSupport extends React.Component {
210245 }
211246 }
212247 } ;
248+ removeLastLayer = ( ) => {
249+ if ( this . lastLayer ) {
250+ this . props . map . removeLayer ( this . lastLayer ) ;
251+ }
252+ }
253+ removeArcLayer = ( ) => {
254+ if ( this . arcLayer ) {
255+ this . props . map . removeLayer ( this . arcLayer ) ;
256+ }
257+ }
213258}
214259
215260module . exports = MeasurementSupport ;
0 commit comments