@@ -8,88 +8,104 @@ const L = window[LeafletSymbol];
8
8
9
9
class DistanceDisplay extends L . Control {
10
10
controlEl : HTMLElement ;
11
- get line ( ) {
12
- return this . map . previousDistanceLine ;
11
+ get lines ( ) {
12
+ return this . map . previousDistanceLines ;
13
13
}
14
- popups : [ Popup , Popup ] ;
14
+ popups : Popup [ ] = [ ] ;
15
15
textEl : HTMLSpanElement ;
16
+ getPopup ( ) {
17
+ return popup ( this . map , null , {
18
+ permanent : true ,
19
+ className : "leaflet-marker-link-popup" ,
20
+ autoClose : false ,
21
+ closeButton : false ,
22
+ closeOnClick : false ,
23
+ autoPan : false
24
+ } ) ;
25
+ }
16
26
constructor ( opts : L . ControlOptions , public map : BaseMapType ) {
17
27
super ( opts ) ;
18
- this . popups = [
19
- popup ( this . map , null , {
20
- permanent : true ,
21
- className : "leaflet-marker-link-popup" ,
22
- autoClose : false ,
23
- closeButton : false ,
24
- closeOnClick : false ,
25
- autoPan : false
26
- } ) ,
27
- popup ( this . map , null , {
28
- permanent : true ,
29
- className : "leaflet-marker-link-popup" ,
30
- autoClose : false ,
31
- closeButton : false ,
32
- closeOnClick : false ,
33
- autoPan : false
34
- } )
35
- ] ;
36
28
}
37
29
initEvents ( ) {
38
30
this . controlEl . onmouseenter = this . onMouseEnter . bind ( this ) ;
39
31
this . controlEl . onclick = this . onClick . bind ( this ) ;
40
32
this . controlEl . onmouseleave = this . onMouseLeave . bind ( this ) ;
41
33
}
42
34
onMouseEnter ( ) {
43
- if ( this . line ) {
44
- const latlngs = this . line . getLatLngs ( ) as L . LatLng [ ] ;
45
-
46
- this . popups [ 0 ] . setTarget ( latlngs [ 0 ] ) . open (
47
- `[${ latlngs [ 0 ] . lat . toLocaleString ( "en-US" , {
48
- maximumFractionDigits : LAT_LONG_DECIMALS
49
- } ) } , ${ latlngs [ 0 ] . lng . toLocaleString ( "en-US" , {
50
- maximumFractionDigits : LAT_LONG_DECIMALS
51
- } ) } ]`
35
+ if ( this . lines . length ) {
36
+ const latlng = this . lines [ 0 ] . getLatLngs ( ) [ 0 ] as L . LatLng ;
37
+ const start = this . getPopup ( ) . setTarget (
38
+ this . lines [ 0 ] . getLatLngs ( ) [ 0 ] as L . LatLng
52
39
) ;
53
- this . map . leafletInstance . openPopup ( this . popups [ 0 ] . leafletInstance ) ;
54
-
55
- this . popups [ 1 ] . setTarget ( latlngs [ 1 ] ) . open (
56
- `[${ latlngs [ 1 ] . lat . toLocaleString ( "en-US" , {
40
+ start . open (
41
+ `[${ latlng . lat . toLocaleString ( "en-US" , {
57
42
maximumFractionDigits : LAT_LONG_DECIMALS
58
- } ) } , ${ latlngs [ 1 ] . lng . toLocaleString ( "en-US" , {
43
+ } ) } , ${ latlng . lng . toLocaleString ( "en-US" , {
59
44
maximumFractionDigits : LAT_LONG_DECIMALS
60
45
} ) } ]`
61
46
) ;
62
- this . map . leafletInstance . openPopup ( this . popups [ 1 ] . leafletInstance ) ;
47
+ this . popups . push ( start ) ;
48
+ this . map . leafletInstance . openPopup ( start . leafletInstance ) ;
63
49
64
- this . line . setStyle ( { color : "blue" , dashArray : "4 1" } ) ;
65
- this . line . addTo ( this . map . leafletInstance ) ;
50
+ for ( let i = 0 ; i < this . lines . length ; i ++ ) {
51
+ const line = this . lines [ i ] ;
52
+ const latlngs = line . getLatLngs ( ) as L . LatLng [ ] ;
53
+
54
+ const display = this . map . distanceAlongPolylines (
55
+ this . lines . slice ( 0 , i + 1 )
56
+ ) ;
57
+ const segment = this . map . distanceAlongPolylines ( [ line ] ) ;
58
+
59
+ const popup = this . getPopup ( ) . setTarget ( latlngs [ 1 ] ) ;
60
+ const element = createDiv ( ) ;
61
+ element . createSpan ( { text : `${ display } (${ segment } )` } ) ;
62
+ element . createEl ( "br" ) ;
63
+ element . createSpan ( {
64
+ text : ` [${ latlngs [ 1 ] . lat . toLocaleString ( "en-US" , {
65
+ maximumFractionDigits : LAT_LONG_DECIMALS
66
+ } ) } , ${ latlngs [ 1 ] . lng . toLocaleString ( "en-US" , {
67
+ maximumFractionDigits : LAT_LONG_DECIMALS
68
+ } ) } ]`
69
+ } ) ;
70
+ popup . open ( element ) ;
71
+ this . map . leafletInstance . openPopup ( popup . leafletInstance ) ;
72
+ this . popups . push ( popup ) ;
73
+
74
+ line . setStyle ( { color : "blue" , dashArray : "4 1" } ) ;
75
+ line . addTo ( this . map . leafletInstance ) ;
76
+ }
66
77
}
67
78
}
68
79
onClick ( evt : MouseEvent ) {
69
80
evt . stopPropagation ( ) ;
70
81
evt . preventDefault ( ) ;
71
82
72
- if ( this . line ) {
73
- this . map . leafletInstance . fitBounds (
74
- L . latLngBounds (
75
- this . line . getLatLngs ( ) [ 0 ] as L . LatLng ,
76
- this . line . getLatLngs ( ) [ 1 ] as L . LatLng
77
- ) ,
78
- {
79
- duration : 0.5 ,
80
- easeLinearity : 0.1 ,
81
- animate : true ,
82
- padding : [ 5 , 5 ]
83
- }
84
- ) ;
83
+ if ( this . lines . length ) {
84
+ const group = new L . FeatureGroup ( ) ;
85
+ for ( const line of this . lines ) {
86
+ new L . Polyline ( [
87
+ line . getLatLngs ( ) [ 0 ] as L . LatLng ,
88
+ line . getLatLngs ( ) [ 1 ] as L . LatLng
89
+ ] ) . addTo ( group ) ;
90
+ }
91
+ this . map . leafletInstance . fitBounds ( group . getBounds ( ) , {
92
+ duration : 0.5 ,
93
+ easeLinearity : 0.1 ,
94
+ animate : true ,
95
+ padding : [ 5 , 5 ]
96
+ } ) ;
85
97
}
86
98
}
87
99
onMouseLeave ( ) {
88
- if ( this . line ) {
89
- this . line . remove ( ) ;
100
+ if ( this . lines ) {
101
+ for ( const line of this . lines ) {
102
+ line . remove ( ) ;
103
+ }
104
+ }
105
+ for ( const popup of this . popups ) {
106
+ this . map . leafletInstance . closePopup ( popup . leafletInstance ) ;
90
107
}
91
- this . map . leafletInstance . closePopup ( this . popups [ 0 ] . leafletInstance ) ;
92
- this . map . leafletInstance . closePopup ( this . popups [ 1 ] . leafletInstance ) ;
108
+ this . popups = [ ] ;
93
109
}
94
110
onAdd ( ) {
95
111
/* this.map = map; */
0 commit comments