2024-12-11 17:32:48 +01:00
/ * *
* rechercher les bornes de recharge ,
* afficher des cercles colorés selon la puissance max de la station
* lister les bornes trouvées dans la page
* @ type { boolean }
* /
2024-12-17 11:53:02 +01:00
import config from './config.js'
import utils from './utils.js'
2024-12-18 13:40:58 +01:00
import colorUtils from './color-utils.js'
2024-12-28 17:59:15 +01:00
import { sendToJOSM , createJOSMEditLink } from './editor.js'
2024-12-11 23:13:15 +01:00
2024-12-23 00:26:13 +01:00
let geojsondata ;
let lastLatLng ;
2024-12-22 23:39:17 +01:00
2024-10-17 15:01:47 +02:00
// serveurs de tuiles: https://wiki.openstreetmap.org/wiki/Tile_servers
// https://stamen-tiles.a.ssl.fastly.net/toner/{z}/{x}/{y}.png
// https://a.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png
2024-10-18 00:47:01 +02:00
// https://tile.openstreetmap.org/{z}/{x}/{y}.png
// 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png'
2024-12-22 23:39:17 +01:00
2024-10-17 15:01:47 +02:00
// Créer la carte centrée sur Rouen
2024-12-14 17:39:17 +01:00
// Liste des 20 villes les plus peuplées de France avec leurs coordonnées géographiques
2024-12-17 11:53:02 +01:00
2024-12-14 17:39:17 +01:00
// Initialisation de la carte avec la vue centrée sur la ville choisie
2024-12-18 13:40:58 +01:00
let map = L . map ( 'map' )
L . control . scale ( ) . addTo ( map )
2024-12-16 12:54:15 +01:00
2024-12-22 00:02:33 +01:00
/ * *
* filtres à toggle par des boutons dans la page
* à appliquer à chaque rafraîchissement des points geojson
* TODO : make buttons and filter in refresh circles
* /
2024-12-22 23:39:17 +01:00
let filterStatesAvailable = [ 'hide' , 'show' , 'showOnly' ]
2024-12-22 00:02:33 +01:00
let display _type2 _sockets = 'show' ;
let display _type2 _combo _sockets = 'show' ;
let display _unknown _max _power _station = 'show' ;
let display _known _max _power _station = 'show' ;
let display _type2 _combo _sockets _with _cable = 'show' ;
let display _lower _than _50kw = 'show' ;
let display _higer _than _50kw = 'show' ;
let display _lower _than _200kw = 'show' ;
let display _higer _than _200kw = 'show' ;
let display _chelou = 'show' ; // les stations avec une valeur suspecte, plus de 400kW
function setRandomView ( ) {
2024-12-28 17:59:15 +01:00
let randomCity = utils . cities [ Math . floor ( Math . random ( ) * utils . cities . length ) ] ;
map = map . setView ( randomCity . coords , config . initialZoom ) ;
2024-12-16 12:54:15 +01:00
}
2024-12-18 13:40:58 +01:00
2024-12-22 00:02:33 +01:00
function setCoordinatesOfLeafletMapFromQueryParameters ( ) {
2024-12-28 17:59:15 +01:00
const urlParams = new URLSearchParams ( window . location . href ) ;
const lat = urlParams . get ( 'lat' ) ;
const lng = urlParams . get ( 'lng' ) ;
const zoom = urlParams . get ( 'zoom' ) ;
2024-12-22 00:02:33 +01:00
if ( lat && lng && zoom ) {
2024-12-28 17:59:15 +01:00
map = map . setView ( [ lat , lng ] , zoom ) ;
2024-12-22 00:02:33 +01:00
} else {
2024-12-28 17:59:15 +01:00
console . error ( 'Les paramètres de coordonnées et de zoom doivent être présents dans l\'URL.' ) ;
setRandomView ( ) ;
2024-12-22 00:02:33 +01:00
}
2024-12-16 12:54:15 +01:00
}
2024-12-22 00:02:33 +01:00
function updateURLWithMapCoordinatesAndZoom ( ) {
// Récupère les coordonnées et le niveau de zoom de la carte
const center = map . getCenter ( )
const zoom = map . getZoom ( )
2024-12-16 12:54:15 +01:00
2024-12-22 00:02:33 +01:00
// Construit l'URL avec les paramètres de coordonnées et de zoom
const url = ` #coords=1&lat= ${ center . lat } &lng= ${ center . lng } &zoom= ${ zoom } `
// Met à jour l'URL de la page
history . replaceState ( null , null , url )
2024-12-16 12:54:15 +01:00
}
2024-12-28 17:00:21 +01:00
let all _stations _markers = L . layerGroup ( ) . addTo ( map ) // layer group pour tous les marqueurs
// let stations_much_speed_wow = L.layerGroup().addTo(map) // layer group des stations rapides
2024-12-22 23:39:17 +01:00
var osm = L . tileLayer ( config . tileServers . osm , {
2024-12-22 00:02:33 +01:00
attribution : config . osmMention + '© <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors'
2024-12-14 18:00:09 +01:00
} )
2024-12-22 23:39:17 +01:00
var cycle = L . tileLayer ( config . tileServers . cycle , {
2024-12-22 00:02:33 +01:00
attribution : config . osmMention + '© <a href="https://www.opencyclemap.org/">OpenCycleMap</a> contributors'
2024-12-14 18:00:09 +01:00
} )
2024-12-22 23:39:17 +01:00
var transport = L . tileLayer ( config . tileServers . transport , {
2024-12-22 00:02:33 +01:00
attribution : config . osmMention
2024-12-14 18:00:09 +01:00
} )
let tileGrey =
2024-12-22 23:39:17 +01:00
L . tileLayer ( config . tileServers . cartodb , {
2024-12-22 00:02:33 +01:00
attribution : config . osmMention
} )
2024-12-14 18:00:09 +01:00
let stamen =
2024-12-22 23:39:17 +01:00
L . tileLayer ( config . tileServers . stamen , {
2024-12-22 00:02:33 +01:00
attribution : config . osmMention
} )
2024-12-14 18:00:09 +01:00
var baseLayers = {
2024-12-22 00:02:33 +01:00
'Grey' : tileGrey ,
'Stamen' : stamen ,
'OpenStreetMap' : osm ,
// 'OpenCycleMap': cycle,
'Transport' : transport
2024-12-14 18:00:09 +01:00
}
2024-12-23 00:26:13 +01:00
let overlays = {
stations _bof : all _stations _markers
2024-12-28 17:00:21 +01:00
}
2024-10-17 15:01:47 +02:00
2024-12-22 00:02:33 +01:00
const layerControl = L . control . layers ( baseLayers , overlays , { collapsed : true } ) . addTo ( map )
2024-12-14 18:08:00 +01:00
tileGrey . addTo ( map )
2024-12-11 23:13:15 +01:00
2024-12-28 17:00:21 +01:00
2024-12-22 00:02:33 +01:00
function buildOverpassApiUrl ( map , overpassQuery ) {
2024-12-28 17:59:15 +01:00
let baseUrl = 'https://overpass-api.de/api/interpreter' ;
const kilometersMarginForLoading = 2 ;
const marginInDegrees = kilometersMarginForLoading / 111 ;
const south = map . getBounds ( ) . getSouth ( ) - marginInDegrees ;
const west = map . getBounds ( ) . getWest ( ) - marginInDegrees ;
const north = map . getBounds ( ) . getNorth ( ) + marginInDegrees ;
const east = map . getBounds ( ) . getEast ( ) + marginInDegrees ;
let bounds = south + ',' + west + ',' + north + ',' + east ;
let resultUrl , query = '' ;
2024-10-17 15:01:47 +02:00
2024-12-22 00:02:33 +01:00
if ( config . overrideQuery ) {
query = ` ?data=[out:json][timeout:15];(
2024-12-21 22:58:50 +01:00
nwr [ amenity = charging _station ] ( $ { bounds } ) ;
2024-12-28 17:59:15 +01:00
) ; out body geom ; ` ;
2024-12-22 00:02:33 +01:00
} else {
2024-12-28 17:59:15 +01:00
let nodeQuery = 'node[' + overpassQuery + '](' + bounds + ');' ;
let wayQuery = 'way[' + overpassQuery + '](' + bounds + ');' ;
let relationQuery = 'relation[' + overpassQuery + '](' + bounds + ');' ;
query = '?data=[out:json][timeout:15];(' + nodeQuery + wayQuery + relationQuery + ');out body geom;' ;
2024-12-22 00:02:33 +01:00
}
2024-12-28 17:59:15 +01:00
resultUrl = baseUrl + query ;
return resultUrl ;
2024-12-28 17:00:21 +01:00
}
2024-10-17 15:01:47 +02:00
2024-12-28 17:59:15 +01:00
2024-10-17 15:01:47 +02:00
2024-12-22 00:02:33 +01:00
function supprimerMarqueurs ( ) {
2024-12-28 17:59:15 +01:00
all _stations _markers . clearLayers ( ) ;
2024-12-22 00:02:33 +01:00
map . eachLayer ( ( layer ) => {
if ( layer instanceof L . Marker ) {
2024-12-28 17:59:15 +01:00
layer . remove ( ) ;
2024-12-22 00:02:33 +01:00
}
2024-12-28 17:59:15 +01:00
} ) ;
2024-10-18 00:33:25 +02:00
}
2024-12-11 23:13:15 +01:00
let coef _reduction _bars = 0.8
2024-10-17 15:01:47 +02:00
2024-12-22 00:02:33 +01:00
function calculerPourcentage ( partie , total , reduc ) {
if ( total === 0 ) {
return 'Division par zéro impossible'
}
let coef _reduction = 1
if ( reduc ) {
coef _reduction = coef _reduction _bars
}
return ( ( partie / total ) * 100 * coef _reduction ) . toFixed ( 1 )
2024-12-11 23:13:15 +01:00
}
2024-12-22 00:02:33 +01:00
function displayStatsFromGeoJson ( resultAsGeojson ) {
2024-12-28 17:59:15 +01:00
let count = resultAsGeojson . features . length ;
let count _station _output = 0 ;
let count _ref _eu = 0 ;
let output _more _than _300 = 0 ;
let output _more _than _200 = 0 ;
let output _more _than _100 = 0 ;
let output _more _than _50 = 0 ;
let count _station _outputoutput _between _1 _and _50 = 0 ;
let count _output _unknown = 0 ;
let count _estimated _type2combo = 0 ;
let count _found _type2combo = 0 ;
let count _found _type2 = 0 ;
2024-12-22 00:02:33 +01:00
resultAsGeojson . features . map ( feature => {
2024-12-28 17:59:15 +01:00
let found _type2 _combo = false ;
let found _type2 = false ;
let keys _of _object = Object . keys ( feature . properties . tags ) ;
2024-12-22 00:02:33 +01:00
keys _of _object . map ( tagKey => {
if ( tagKey . indexOf ( 'type2_combo' ) !== - 1 ) {
2024-12-28 17:59:15 +01:00
found _type2 _combo = true ;
2024-12-22 00:02:33 +01:00
}
if ( tagKey . indexOf ( 'type2' ) !== - 1 ) {
2024-12-28 17:59:15 +01:00
found _type2 = true ;
2024-12-22 00:02:33 +01:00
}
2024-12-28 17:59:15 +01:00
} ) ;
let outputPower = utils . guessOutputPowerFromFeature ( feature ) ;
2024-12-22 00:02:33 +01:00
if ( found _type2 _combo ) {
2024-12-28 17:59:15 +01:00
count _found _type2combo ++ ;
2024-12-22 00:02:33 +01:00
}
if ( found _type2 ) {
2024-12-28 17:59:15 +01:00
count _found _type2 ++ ;
2024-12-22 00:02:33 +01:00
}
if ( outputPower == 0 ) {
2024-12-28 17:59:15 +01:00
count _output _unknown ++ ;
2024-12-22 00:02:33 +01:00
}
if ( outputPower >= 200 && ! found _type2 _combo ) {
2024-12-28 17:59:15 +01:00
count _estimated _type2combo ++ ;
2024-12-22 00:02:33 +01:00
}
if ( outputPower > 0 && outputPower < 50 ) {
2024-12-28 17:59:15 +01:00
count _station _outputoutput _between _1 _and _50 ++ ;
2024-12-22 00:02:33 +01:00
}
if ( outputPower >= 50 && outputPower < 100 ) {
2024-12-28 17:59:15 +01:00
output _more _than _50 ++ ;
2024-12-22 00:02:33 +01:00
} else if ( outputPower >= 100 && outputPower < 200 ) {
2024-12-28 17:59:15 +01:00
output _more _than _100 ++ ;
2024-12-22 00:02:33 +01:00
} else if ( outputPower >= 200 && outputPower < 300 ) {
2024-12-28 17:59:15 +01:00
output _more _than _200 ++ ;
2024-12-22 00:02:33 +01:00
} else if ( outputPower >= 300 ) {
2024-12-28 17:59:15 +01:00
feature . properties . puissance _haute = true ;
output _more _than _300 ++ ;
2024-12-22 00:02:33 +01:00
}
if ( feature . properties . tags [ 'charging_station:output' ] ) {
2024-12-28 17:59:15 +01:00
count _station _output ++ ;
2024-12-22 00:02:33 +01:00
}
if ( feature . properties . tags [ 'ref:EU:EVSE' ] ) {
2024-12-28 17:59:15 +01:00
count _ref _eu ++ ;
2024-12-22 00:02:33 +01:00
}
2024-12-28 17:59:15 +01:00
} ) ;
2024-12-22 00:02:33 +01:00
let bar _powers = ` <div class="bars-container">
2024-12-11 23:13:15 +01:00
< div class = "bar color-unknown" style = "width: ${calculerPourcentage(count_output_unknown, count, true)}%" > $ { count _output _unknown } < / d i v >
2024-12-22 19:07:09 +01:00
< div class = "bar color-power-lesser-than-50" style = "width: ${calculerPourcentage(count_station_outputoutput_between_1_and_50, count, true)}%" > $ { count _station _outputoutput _between _1 _and _50 ? count _station _outputoutput _between _1 _and _50 : '' } < / d i v >
< div class = "bar color-power-lesser-than-100" style = "width: ${calculerPourcentage(output_more_than_50, count, true)}%" > $ { output _more _than _50 ? output _more _than _50 : '' } < / d i v >
< div class = "bar color-power-lesser-than-200" style = "width: ${calculerPourcentage(output_more_than_100, count, true)}%" > $ { output _more _than _100 ? output _more _than _100 : '' } < / d i v >
2024-12-22 23:39:17 +01:00
< div class = "bar color-power-lesser-than-300" style = "width: ${calculerPourcentage(output_more_than_200, count, true)}%" > $ { output _more _than _200 ? output _more _than _200 : '' | '' } < / d i v >
2024-12-22 19:07:09 +01:00
< div class = "bar color-power-lesser-than-max" style = "width: ${calculerPourcentage(output_more_than_300, count, true)}%" > $ { output _more _than _300 ? output _more _than _300 : '' } < / d i v >
2024-12-28 17:59:15 +01:00
< / d i v > ` ;
2024-12-11 23:13:15 +01:00
2024-12-28 17:00:21 +01:00
let stats _content = ` <div class="stats-table">
< table >
< tr >
< th > Type < / t h >
< th > Nombre < / t h >
< th > Pourcentage < / t h >
< / t r >
< tr >
< td > Puissance inconnue < / t d >
< td > $ { count _output _unknown } < / t d >
< td > $ { calculerPourcentage ( count _output _unknown , count ) } % < / t d >
< / t r >
< tr >
< td > 1 - 50 kW < / t d >
< td > $ { count _station _outputoutput _between _1 _and _50 } < / t d >
< td > $ { calculerPourcentage ( count _station _outputoutput _between _1 _and _50 , count ) } % < / t d >
< / t r >
< tr >
< td > 50 - 100 kW < / t d >
< td > $ { output _more _than _50 } < / t d >
< td > $ { calculerPourcentage ( output _more _than _50 , count ) } % < / t d >
< / t r >
< tr >
< td > 100 - 200 kW < / t d >
< td > $ { output _more _than _100 } < / t d >
< td > $ { calculerPourcentage ( output _more _than _100 , count ) } % < / t d >
< / t r >
< tr >
< td > 200 - 300 kW < / t d >
< td > $ { output _more _than _200 } < / t d >
< td > $ { calculerPourcentage ( output _more _than _200 , count ) } % < / t d >
< / t r >
< tr >
< td > 300 + kW < / t d >
< td > $ { output _more _than _300 } < / t d >
< td > $ { calculerPourcentage ( output _more _than _300 , count ) } % < / t d >
< / t r >
< / t a b l e >
2024-12-28 17:59:15 +01:00
< / d i v > ` ;
2024-12-28 17:00:21 +01:00
2024-12-28 17:59:15 +01:00
$ ( '#found_charging_stations' ) . html ( stats _content ) ;
$ ( '#bars_power' ) . html ( bar _powers ) ;
2024-12-11 23:13:15 +01:00
}
2024-12-11 17:32:48 +01:00
2024-12-22 00:02:33 +01:00
function bindEventsOnJosmRemote ( ) {
2024-12-28 17:00:21 +01:00
let josm _remote _buttons = $ ( ` #sendToJOSM ` )
2024-12-22 00:02:33 +01:00
$ ( josm _remote _buttons [ 0 ] ) . on ( 'click' , ( ) => {
let josm _link = $ ( josm _remote _buttons [ 0 ] ) . attr ( 'data-href' )
$ . get ( josm _link , ( res ) => {
console . log ( 'res' , res )
} )
} )
2024-12-12 00:27:27 +01:00
}
2024-12-14 18:00:09 +01:00
2024-12-22 00:02:33 +01:00
2024-12-23 00:02:46 +01:00
function displayPointsFromApi ( points ) {
if ( points ) {
2024-12-28 17:59:15 +01:00
geojsondata = osmtogeojson ( points ) ;
2024-12-23 00:02:46 +01:00
}
2024-12-22 00:02:33 +01:00
2024-12-28 17:59:15 +01:00
displayStatsFromGeoJson ( geojsondata ) ;
2024-12-22 00:02:33 +01:00
let resultLayer = L . geoJson ( geojsondata , {
style : function ( feature ) {
2024-12-28 17:59:15 +01:00
return { color : '#f00' } ;
2024-12-22 00:02:33 +01:00
} ,
filter : function ( feature , layer ) {
2024-12-28 17:59:15 +01:00
let isPolygon = ( feature . geometry ) && ( feature . geometry . type !== undefined ) && ( feature . geometry . type === 'Polygon' ) ;
2024-12-22 00:02:33 +01:00
if ( isPolygon ) {
2024-12-28 17:59:15 +01:00
feature . geometry . type = 'Point' ;
let polygonCenter = L . latLngBounds ( feature . geometry . coordinates [ 0 ] ) . getCenter ( ) ;
feature . geometry . coordinates = [ polygonCenter . lat , polygonCenter . lng ] ;
2024-12-22 00:02:33 +01:00
}
2024-12-28 17:59:15 +01:00
return true ;
2024-12-22 00:02:33 +01:00
} ,
onmoveend : function ( event ) {
2024-12-28 17:59:15 +01:00
// console.log('déplacement terminé');
2024-12-22 00:02:33 +01:00
} ,
onzoomend : function ( event ) {
2024-12-28 17:59:15 +01:00
supprimerMarqueurs ( ) ;
displayPointsFromApi ( ) ;
2024-12-22 00:02:33 +01:00
} ,
2024-12-22 23:39:17 +01:00
onEachFeature : eachFeature ,
2024-12-28 17:59:15 +01:00
} ) ;
2024-12-22 23:39:17 +01:00
}
function makePopupOfFeature ( feature ) {
let popupContent = ''
popupContent += '<div class="sockets-list" >'
let type2 = feature . properties . tags [ 'socket:type2' ]
let type2 _combo = feature . properties . tags [ 'socket:type2_combo' ]
if ( type2 ) {
popupContent += ' <img class="icon-img" src="img/Type2_socket.svg" alt="prise de type 2">'
if ( type2 !== 'yes' ) {
popupContent += '<span class="socket-counter">x ' + type2 + '</span>'
}
}
if ( feature . properties . tags [ 'socket:type2_combo' ] ) {
popupContent += ' <img class="icon-img" src="img/type2_combo.svg" alt="prise de type 2 combo CCS">'
if ( type2 _combo !== 'yes' ) {
popupContent += '<span class="socket-counter">x ' + type2 _combo + '</span>'
}
}
popupContent += '</div>'
popupContent += '<div class="key-values" >'
// ne montrer que certains champs dans la popup
2024-12-28 17:00:21 +01:00
config . tags _to _display _in _popup . forEach ( function ( key ) {
if ( config . tags _to _display _in _popup . indexOf ( key ) ) {
2024-12-22 23:39:17 +01:00
let value = feature . properties . tags [ key ]
if ( value ) {
if ( value . indexOf ( 'http' ) !== - 1 ) {
value = '<a href="' + value + '">' + value + '</a>'
2024-12-22 00:02:33 +01:00
}
2024-12-22 23:39:17 +01:00
popupContent = popupContent + '<br/><strong class="popup-key">' + key + ' :</strong><span class="popup-value">' + value + '</span>'
2024-12-22 00:02:33 +01:00
}
2024-12-22 23:39:17 +01:00
}
} )
popupContent += '</div>'
return popupContent ;
}
function eachFeature ( feature , layer ) {
2024-12-28 17:59:15 +01:00
let link _josm = createJOSMEditLink ( feature ) ;
2024-12-22 23:39:17 +01:00
2024-12-28 17:59:15 +01:00
let popupContent = makePopupOfFeature ( feature ) ;
layer . bindPopup ( popupContent ) ;
2024-12-22 23:39:17 +01:00
2024-12-28 17:59:15 +01:00
let outPowerGuessed = utils . guessOutputPowerFromFeature ( feature ) ;
let color = colorUtils . getColor ( feature ) ;
let displayOutPowerGuessed = '? kW' ;
2024-12-22 23:39:17 +01:00
if ( outPowerGuessed ) {
2024-12-28 17:59:15 +01:00
displayOutPowerGuessed = outPowerGuessed + ' kW max' ;
2024-12-22 23:39:17 +01:00
}
if ( ! popupContent ) {
popupContent = ` <span class="no-data"> Aucune information renseignée,
2024-12-28 17:59:15 +01:00
< a class = "edit-button" href = "https://www.openstreetmap.org/edit?editor=remote&node=${feature.properties.id}" > ajoutez la dans OpenStreetMap ! < / a > < / s p a n > ` ;
2024-12-22 23:39:17 +01:00
}
2024-12-14 18:00:09 +01:00
2024-12-22 23:39:17 +01:00
let html = ` <a href="https://www.openstreetmap.org/directions?from=&to= ${ feature . geometry . coordinates [ 1 ] } , ${ feature . geometry . coordinates [ 0 ] } &engine=fossgis_osrm_car#map=14/ ${ feature . geometry . coordinates [ 1 ] } / ${ feature . geometry . coordinates [ 0 ] } " class="navigation-link by-car" title="itinéraire en voiture vers cette station"> 🚗</a><a href="https://www.openstreetmap.org/directions?from=&to= ${ feature . geometry . coordinates [ 1 ] } , ${ feature . geometry . coordinates [ 0 ] } &engine=fossgis_osrm_bike#map=14/ ${ feature . geometry . coordinates [ 1 ] } / ${ feature . geometry . coordinates [ 0 ] } " class="navigation-link by-car" title="itinéraire en vélo vers cette station">🚴♀️</a><a href="https://www.openstreetmap.org/directions?from=&to= ${ feature . geometry . coordinates [ 1 ] } , ${ feature . geometry . coordinates [ 0 ] } &engine=fossgis_osrm_foot#map=14/ ${ feature . geometry . coordinates [ 1 ] } / ${ feature . geometry . coordinates [ 0 ] } " class="navigation-link by-car" title="itinéraire à pied vers cette station">👠</a>
2024-12-28 17:59:15 +01:00
< a class = "edit-button" href = "https://www.openstreetmap.org/edit?editor=id&node=${feature.properties.id}" > ✏ ️ < / a > < a c l a s s = " e d i t - b u t t o n j o s m " d a t a - h r e f = " $ { l i n k _ j o s m } " h r e f = " # " > J O S M < / a > < s p a n c l a s s = " c o l o r - i n d i c a t i o n " s t y l e = " b a c k g r o u n d - c o l o r : $ { c o l o r } ; " > $ { d i s p l a y O u t P o w e r G u e s s e d } < / s p a n > < s p a n c l a s s = " p o p u p - c o n t e n t " > $ { p o p u p C o n t e n t } < / s p a n > ` ;
2024-11-26 21:31:43 +01:00
2024-12-28 17:59:15 +01:00
let zoom = map . getZoom ( ) ;
let radius = 20 ;
let opacity = 0.5 ;
let ratio _circle = 10 ;
2024-12-22 23:39:17 +01:00
if ( zoom < 13 ) {
2024-12-28 17:59:15 +01:00
ratio _circle = 5 ;
2024-12-22 23:39:17 +01:00
} else if ( zoom < 15 ) {
2024-12-28 17:59:15 +01:00
ratio _circle = 1 ;
opacity = 0.25 ;
2024-12-22 23:39:17 +01:00
} else if ( zoom <= 16 ) {
2024-12-28 17:59:15 +01:00
ratio _circle = 0.5 ;
2024-12-22 23:39:17 +01:00
} else if ( zoom <= 18 ) {
2024-12-28 17:59:15 +01:00
ratio _circle = 0.25 ;
2024-12-22 23:39:17 +01:00
}
2024-12-28 17:59:15 +01:00
2024-12-23 00:26:13 +01:00
if ( ! layer . _latlng ) {
if ( lastLatLng ) {
2024-12-28 17:59:15 +01:00
layer . _latlng = lastLatLng ;
2024-12-23 00:26:13 +01:00
}
} else {
2024-12-28 17:59:15 +01:00
lastLatLng = layer . _latlng ;
2024-12-23 00:26:13 +01:00
}
2024-12-22 23:39:17 +01:00
if ( ! outPowerGuessed ) {
2024-12-28 17:59:15 +01:00
radius = radius * ratio _circle ;
2024-12-22 23:39:17 +01:00
} else {
2024-12-28 17:59:15 +01:00
radius = outPowerGuessed * ratio _circle ;
2024-12-22 23:39:17 +01:00
}
2024-12-22 00:02:33 +01:00
2024-12-22 23:39:17 +01:00
let circle = L . circle ( layer . _latlng , {
color : color ,
fillColor : color ,
fillOpacity : opacity ,
colorOpacity : opacity ,
radius : radius
2024-12-28 17:59:15 +01:00
} ) . addTo ( all _stations _markers ) ;
2024-12-22 23:39:17 +01:00
if ( zoom > 15 ) {
let circle _center = L . circle ( layer . _latlng , {
color : 'black' ,
fillColor : color ,
fillOpacity : 1 ,
radius : 0.1
2024-12-23 00:26:13 +01:00
} ) . addTo ( all _stations _markers ) ;
2024-12-22 23:39:17 +01:00
}
2024-12-22 00:02:33 +01:00
2024-12-28 17:59:15 +01:00
circle . bindPopup ( html ) ;
2024-12-22 23:39:17 +01:00
circle . on ( {
mouseover : function ( ) {
2024-12-28 17:59:15 +01:00
this . openPopup ( ) ;
bindEventsOnJosmRemote ( ) ;
2024-12-22 23:39:17 +01:00
} ,
mouseout : function ( ) {
2024-12-28 17:59:15 +01:00
// setTimeout(() => this.closePopup(), 15000);
2024-12-22 23:39:17 +01:00
} ,
click : function ( ) {
2024-12-28 17:59:15 +01:00
this . openPopup ( ) ;
bindEventsOnJosmRemote ( ) ;
2024-12-22 00:02:33 +01:00
} ,
2024-12-28 17:59:15 +01:00
} ) ;
2024-10-17 15:01:47 +02:00
}
2024-12-22 00:02:33 +01:00
function makeCssClassFromTags ( tags ) {
let tagKeys = Object . keys ( tags )
if ( ! tags ) {
return ''
}
let listOfClasses = [ ]
2024-10-17 15:01:47 +02:00
2024-12-22 00:02:33 +01:00
tagKeys . forEach ( ( element ) => {
listOfClasses . push ( 'tag-' + element + '_' + tags [ element ] . replace ( ':' , '--' ) . replace ( ' ' , '-' ) )
} )
2024-10-17 15:01:47 +02:00
2024-12-22 00:02:33 +01:00
return listOfClasses . join ( ' ' )
2024-10-17 15:01:47 +02:00
}
2024-12-22 00:02:33 +01:00
function getIconFromTags ( tags ) {
let iconFileName = ''
// let iconFileName = 'icon_restaurant.png';
if ( tags [ 'man_made' ] ) {
iconFileName = 'fountain.png'
}
return iconFileName
2024-10-17 15:01:47 +02:00
}
2024-12-22 00:02:33 +01:00
function toggleMinPower ( showHighPower ) {
2024-12-28 17:59:15 +01:00
showHighPower = ! showHighPower ;
addFilteredMarkers ( showHighPower ) ;
this . textContent = showHighPower ? 'Montrer puissance haute' : 'Montrer puissance normale' ;
2024-12-11 23:13:15 +01:00
}
2024-12-22 00:02:33 +01:00
function addFilteredMarkers ( showHighPower ) {
2024-12-28 17:59:15 +01:00
allMarkers . clearLayers ( ) ;
2024-12-22 00:02:33 +01:00
2024-12-28 17:59:15 +01:00
let counter = 0 ;
2024-12-22 00:02:33 +01:00
geojsondata . features . forEach ( function ( feature ) {
if ( feature . properties . puissance _haute === showHighPower ) {
2024-12-28 17:59:15 +01:00
counter ++ ;
let marker = L . marker ( feature . geometry . coordinates ) . bindPopup ( feature . properties . puissance _haute ? 'Puissance haute' : 'Puissance normale' ) ;
allMarkers . addLayer ( marker ) ;
2024-12-22 00:02:33 +01:00
}
2024-12-28 17:59:15 +01:00
} ) ;
2024-12-11 17:32:48 +01:00
}
2024-10-17 15:01:47 +02:00
let isLoading = false
2024-12-22 00:02:33 +01:00
function loadOverpassQuery ( ) {
if ( ! isLoading ) {
2024-12-28 17:59:15 +01:00
isLoading = true ;
$ ( '#spinning_icon' ) . fadeIn ( ) ;
let queryTextfieldValue = $ ( '#query-textfield' ) . val ( ) ;
let overpassApiUrl = buildOverpassApiUrl ( map , queryTextfieldValue ) ;
2024-12-22 00:02:33 +01:00
$ . get ( overpassApiUrl , function ( geoDataPointsFromApi ) {
2024-12-28 17:59:15 +01:00
geojsondata = geoDataPointsFromApi ;
refreshDisplay ( ) ;
$ ( '#spinning_icon' ) . fadeOut ( ) ;
$ ( '#message-loading' ) . fadeOut ( ) ;
isLoading = false ;
} ) ;
2024-12-22 00:02:33 +01:00
}
2024-10-17 15:01:47 +02:00
}
2024-12-22 23:39:17 +01:00
function refreshDisplay ( ) {
2024-12-28 17:59:15 +01:00
supprimerMarqueurs ( ) ;
displayPointsFromApi ( geojsondata ) ;
2024-12-22 23:39:17 +01:00
}
2024-12-22 00:02:33 +01:00
function onMapMoveEnd ( ) {
let center = map . getCenter ( )
let zoom = map . getZoom ( )
let infos = ` Lat: ${ center . lat } , Lon: ${ center . lng } , Zoom : ${ zoom } `
2024-12-28 23:14:38 +01:00
2024-12-28 13:29:29 +01:00
if ( zoom < 10 ) {
$ ( '#zoomMessage' ) . show ( )
2024-12-22 00:02:33 +01:00
} else {
2024-12-28 13:29:29 +01:00
$ ( '#zoomMessage' ) . hide ( )
loadOverpassQuery ( )
2024-12-22 00:02:33 +01:00
}
2024-12-28 23:14:38 +01:00
if ( map . getZoom ( ) > 14 ) {
searchFoodPlaces ( map ) ;
} else {
food _places _markers . clearLayers ( ) ;
}
2024-12-22 00:02:33 +01:00
$ ( '#infos_carte' ) . html ( infos )
2024-12-28 17:00:21 +01:00
// Stocker les dernières coordonnées connues
if ( ! window . lastKnownPosition ) {
window . lastKnownPosition = center ;
updateURLWithMapCoordinatesAndZoom ( ) ;
} else {
// Calculer la distance en km entre l'ancienne et la nouvelle position
const distanceKm = map . distance ( center , window . lastKnownPosition ) / 1000 ;
// Ne mettre à jour que si on s'est déplacé de plus de 2km
if ( distanceKm > 2 ) {
window . lastKnownPosition = center ;
updateURLWithMapCoordinatesAndZoom ( ) ;
}
}
2024-12-16 12:54:15 +01:00
2024-12-12 00:27:27 +01:00
}
2024-12-16 12:54:15 +01:00
setCoordinatesOfLeafletMapFromQueryParameters ( )
2024-12-14 18:00:09 +01:00
$ ( document ) . ready ( function ( ) {
2024-12-28 17:59:15 +01:00
bindEventsOnJosmRemote ( ) ;
onMapMoveEnd ( ) ;
map . on ( 'moveend' , onMapMoveEnd ) ;
$ ( '#spinning_icon' ) . hide ( ) ;
2024-12-22 00:02:33 +01:00
$ ( '#removeMarkers' ) . on ( 'click' , function ( ) {
2024-12-28 17:59:15 +01:00
supprimerMarqueurs ( ) ;
} ) ;
2024-12-22 00:02:33 +01:00
$ ( '#load' ) . on ( 'click' , function ( ) {
2024-12-28 17:59:15 +01:00
loadOverpassQuery ( ) ;
} ) ;
2024-12-28 17:00:21 +01:00
$ ( '#toggleSidePanel' ) . on ( 'click' , function ( ) {
2024-12-28 17:59:15 +01:00
$ ( 'body' ) . toggleClass ( 'side-panel-open' ) ;
} ) ;
2024-12-23 00:09:52 +01:00
$ ( '#chercherButton' ) . on ( 'click' , function ( ) {
2024-12-28 17:59:15 +01:00
supprimerMarqueurs ( ) ;
loadOverpassQuery ( ) ;
} ) ;
2024-12-23 00:30:04 +01:00
$ ( '#setRandomView' ) . on ( 'click' , function ( ) {
2024-12-28 17:59:15 +01:00
setRandomView ( ) ;
loadOverpassQuery ( ) ;
} ) ;
2024-12-22 00:02:33 +01:00
$ ( '#filterUnkown' ) . on ( 'click' , function ( ) {
2024-12-28 17:59:15 +01:00
display _unknown _max _power _station = cycleVariableState ( display _unknown _max _power _station , '#filterUnkown' ) ;
showActiveFilter ( display _unknown _max _power _station , '#filterUnkown' ) ;
refreshDisplay ( ) ;
} ) ;
showActiveFilter ( display _unknown _max _power _station , '#filterUnkown' ) ;
} ) ;
2024-12-16 12:13:19 +01:00
2024-12-22 00:02:33 +01:00
function showActiveFilter ( filterVariableName , selectorId ) {
2024-12-22 23:39:17 +01:00
$ ( selectorId ) . attr ( 'class' , 'filter-state-' + filterVariableName )
2024-12-22 00:02:33 +01:00
}
2024-12-28 17:00:21 +01:00
2024-12-22 00:02:33 +01:00
function cycleVariableState ( filterVariableName , selectorId ) {
2024-12-22 23:39:17 +01:00
console . log ( 'filterVariableName' , filterVariableName , filterStatesAvailable )
2024-12-22 00:02:33 +01:00
if ( filterVariableName ) {
if ( filterVariableName == filterStatesAvailable [ 0 ] ) {
filterVariableName = filterStatesAvailable [ 1 ]
} else if ( filterVariableName == filterStatesAvailable [ 1 ] ) {
filterVariableName = filterStatesAvailable [ 2 ]
} else if ( filterVariableName == filterStatesAvailable [ 2 ] ) {
filterVariableName = filterStatesAvailable [ 0 ]
}
} else {
filterVariableName = filterStatesAvailable [ 0 ]
}
showActiveFilter ( filterVariableName , selectorId )
2024-12-22 23:39:17 +01:00
console . log ( 'filterVariableName after' , filterVariableName )
2024-12-22 00:02:33 +01:00
return filterVariableName
}
2024-12-16 12:13:19 +01:00
2024-12-28 13:29:29 +01:00
// toggle des stats
$ ( '#toggle-stats' ) . on ( 'click' , function ( ) {
$ ( '#found_charging_stations' ) . slideToggle ( ) ;
// Change le symbole de la flèche
let text = $ ( this ) . text ( ) ;
if ( text . includes ( '🔽' ) ) {
$ ( this ) . text ( text . replace ( '🔽' , '🔼' ) ) ;
} else {
$ ( this ) . text ( text . replace ( '🔼' , '🔽' ) ) ;
}
} ) ;
2024-12-28 17:00:21 +01:00
// Ajouter ces variables avec les autres déclarations globales
let food _places _markers = L . layerGroup ( ) ;
const foodIcon = L . divIcon ( {
className : 'food-marker' ,
html : '🍽️' ,
iconSize : [ 20 , 20 ] ,
iconAnchor : [ 10 , 10 ]
} ) ;
// Ajouter cette fonction avec les autres fonctions de recherche
function searchFoodPlaces ( map ) {
const bounds = map . getBounds ( ) ;
const bbox = bounds . getSouth ( ) + ',' + bounds . getWest ( ) + ',' + bounds . getNorth ( ) + ',' + bounds . getEast ( ) ;
const query = `
[ out : json ] [ timeout : 25 ] ;
(
node [ "amenity" = "restaurant" ] ( $ { bbox } ) ;
node [ "amenity" = "cafe" ] ( $ { bbox } ) ;
) ;
out body ;
> ;
out skel qt ; ` ;
const url = ` https://overpass-api.de/api/interpreter?data= ${ encodeURIComponent ( query ) } ` ;
food _places _markers . clearLayers ( ) ;
fetch ( url )
. then ( response => response . json ( ) )
. then ( data => {
const geojson = osmtogeojson ( data ) ;
geojson . features . forEach ( feature => {
const coords = feature . geometry . coordinates ;
const properties = feature . properties ;
const name = properties . tags . name || 'Sans nom' ;
const type = properties . tags . amenity ;
const marker = L . marker ( [ coords [ 1 ] , coords [ 0 ] ] , {
icon : foodIcon
} ) ;
marker . bindPopup ( `
< strong > $ { name } < / s t r o n g > < b r >
Type : $ { type } < br >
$ { properties . tags . cuisine ? 'Cuisine: ' + properties . tags . cuisine : '' }
` );
food _places _markers . addLayer ( marker ) ;
} ) ;
} )
. catch ( error => console . error ( 'Erreur lors de la recherche des restaurants:' , error ) ) ;
}
// Modifier la fonction init pour ajouter le contrôle des couches
function init ( ) {
// ... existing map initialization code ...
// Ajouter le groupe de marqueurs à la carte
food _places _markers . addTo ( map ) ;
$ ( '#found_charging_stations' ) . hide ( ) ;
// Ajouter le contrôle des couches
const overlayMaps = {
"Stations de recharge" : all _stations _markers ,
"Restaurants et cafés" : food _places _markers
} ;
L . control . layers ( null , overlayMaps ) . addTo ( map ) ;
2024-12-28 23:14:38 +01:00
2024-12-28 17:00:21 +01:00
2024-12-28 23:14:38 +01:00
$ ( '#sendToJOSM' ) . on ( 'click' , ( ) => {
sendToJOSM ( map , geojsondata )
. then ( ( ) => {
console . log ( 'Données envoyées à JOSM avec succès !' ) ;
} )
. catch ( ( ) => {
alert ( 'Erreur : JOSM doit être ouvert avec l\'option "Contrôle à distance" activée' ) ;
} ) ;
2024-12-28 17:00:21 +01:00
} ) ;
2024-12-28 17:59:15 +01:00
$ ( '#searchButton' ) . on ( 'click' , searchLocation ) ;
$ ( '#shareUrl' ) . on ( 'click' , copyCurrentUrl ) ;
document . getElementById ( 'searchButton' ) . addEventListener ( 'click' , searchLocation ) ;
}
2024-12-28 22:43:18 +01:00
2024-12-28 17:59:15 +01:00
function copyCurrentUrl ( ) {
const url = window . location . href ;
var dummy = document . createElement ( 'input' ) ,
text = window . location . href ;
2024-12-28 22:43:18 +01:00
2024-12-28 17:59:15 +01:00
document . body . appendChild ( dummy ) ;
dummy . value = text ;
dummy . select ( ) ;
document . execCommand ( 'copy' ) ;
document . body . removeChild ( dummy ) ;
}
function searchLocation ( ) {
const location = document . getElementById ( 'searchLocation' ) . value ;
if ( ! location ) {
alert ( 'Veuillez entrer un lieu à rechercher.' ) ;
return ;
}
const url = ` https://nominatim.openstreetmap.org/search?format=json&q= ${ encodeURIComponent ( location ) } ` ;
fetch ( url )
2024-12-28 22:43:18 +01:00
. then ( response => response . json ( ) )
. then ( data => {
if ( data . length > 0 ) {
const place = data [ 0 ] ;
const lat = parseFloat ( place . lat ) ;
const lon = parseFloat ( place . lon ) ;
map . setView ( [ lat , lon ] , 13 ) ; // Ajustez le niveau de zoom selon vos besoins
2024-12-28 17:59:15 +01:00
} else {
alert ( 'Lieu non trouvé. Veuillez essayer un autre terme de recherche.' ) ;
}
} )
. catch ( error => {
console . error ( 'Erreur lors de la recherche du lieu :' , error ) ;
alert ( 'Erreur lors de la recherche du lieu.' ) ;
} ) ;
2024-12-28 22:43:18 +01:00
}
init ( )