@ -4,7 +4,9 @@ import 'leaflet';
export default class extends Controller {
static values = {
geojson : String ,
overpassResult : String ,
icon : String ,
popupUrl : String ,
}
connect ( ) {
@ -24,9 +26,10 @@ export default class extends Controller {
'danger' : L . divIcon ( { html : iconHtml , className : 'svg-icon text-danger' , iconSize : [ 16 , 16 ] , iconAnchor : [ 8 , 16 ] , } ) ,
'warning' : L . divIcon ( { html : iconHtml , className : 'svg-icon text-warning' , iconSize : [ 16 , 16 ] , iconAnchor : [ 8 , 16 ] , } ) ,
'success' : L . divIcon ( { html : iconHtml , className : 'svg-icon text-success' , iconSize : [ 16 , 16 ] , iconAnchor : [ 8 , 16 ] , } ) ,
'info' : L . divIcon ( { html : iconHtml , className : 'svg-icon text-info' , iconSize : [ 16 , 16 ] , iconAnchor : [ 8 , 16 ] , } ) ,
} ;
var map = L . map ( this . element ) ;
var geojsons , _this = this , map = L . map ( this . element ) ;
L . tileLayer ( 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' , {
maxZoom : 19 ,
attribution : '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
@ -34,7 +37,40 @@ export default class extends Controller {
var layer = L . featureGroup ( ) ;
var geojsons = JSON . parse ( this . geojsonValue ) ;
var layer2 = L . featureGroup ( ) ;
if ( this . overpassResultValue !== '' ) {
geojsons = JSON . parse ( this . overpassResultValue ) ;
if ( geojsons . elements . length > 0 ) {
geojsons . elements . forEach ( function ( element ) {
element . members . forEach ( function ( member ) {
const polygon = L . polyline ( member . geometry . map ( function ( coord ) { return L . latLng ( coord . lat , coord . lon ) } ) , {
color : '#0dcaf0' ,
weight : 6 ,
opacity : 0.8 ,
} ) . addTo ( layer2 ) . bindPopup ( L . popup ( {
overpassElement : element ,
} ) . setContent ( '…' ) ) ;
} ) ;
} ) ;
layer2 . on ( 'popupopen' , function ( event ) {
var element = event . popup . options . overpassElement ;
delete element . members ;
fetch ( _this . popupUrlValue + '?' + ( new URLSearchParams ( {
'element' : JSON . stringify ( element ) ,
} ) ) )
. then ( function ( response ) {
return response . text ( ) ;
} )
. then ( function ( text ) {
event . popup . setContent ( text ) ;
} ) ;
} ) ;
layer2 . addTo ( layer ) ;
}
}
var layer1 = L . featureGroup ( ) ;
geojsons = JSON . parse ( this . geojsonValue ) ;
if ( geojsons . length > 0 ) {
geojsons . forEach ( function ( geojson ) {
@ -50,7 +86,7 @@ export default class extends Controller {
}
return { color : color } ;
}
} ) . bindTooltip ( feature0 . name ) . addTo ( layer ) . on ( 'click' , function ( event ) {
} ) . bindTooltip ( feature0 . name ) . addTo ( layer1 ) . on ( 'click' , function ( event ) {
window . location . href = event . layer . feature . properties . url ;
} ) ;
@ -58,14 +94,23 @@ export default class extends Controller {
icon : icons [ feature0 . color ] ,
title : feature0 . name ,
clickUrl : feature0 . url ,
} ) . addTo ( layer ) . on ( 'click' , function ( event ) {
} ) . addTo ( layer1 ) . on ( 'click' , function ( event ) {
window . location . href = event . target . options . clickUrl ;
} ) ;
} ) ;
layer . addTo ( map ) ;
map . fitBounds ( layer . getBounds ( ) ) ;
layer1 . addTo ( layer ) ;
}
layer . addTo ( map ) ;
if ( this . overpassResultValue !== '' ) {
L . control . layers ( { } , {
'Overpass' : layer2 ,
'Tâches' : layer1 ,
} ) . addTo ( map ) ;
}
map . fitBounds ( layer . getBounds ( ) ) ;
}
}