1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
let carte = {
lat: 43.6044,
lng: 1.4442,
zoom: 13,
mapContainer: 'mapid',
mymap: '',
displayMap: '',
addTo: '',
idMap: 'mapbox.streets',
accessToken: 'pk.eyJ1IjoidGhyb3VkIiwiYSIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
layer: 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}',
init() {
this.mymap = L.map(this.mapContainer, {center: [this.lat, this.lng], zoom: this.zoom});
},
display() {
this.displayMap = L.tileLayer(this.layer, {id: this.idMap, accessToken: this.accessToken})
},
ajout() {
this.addTo = addTo(this.mymap)
},
callback : function(response) {
response = JSON.parse(response);
response.forEach(function (info) {
L.marker(
[info.position.lat, info.position.lng],
{
"jcdecauxInfo": info}// On ajoute une option au marker qui a pour propriété jcdecauxInfo et comme valeur info
)
.on('click', onMarkerClick)// Fonction d'appel lors du click
.addTo(mymap)
.bindPopup(info.name);
});
},
onMarkerClick : function(arg) {
// Récupération du marker concerné
let marker = arg.target;
// Récupération des infos
let info = marker.options.jcdecauxInfo;
let address = info.address;
let bikeStands = info.bike_stands;
let availableBikes = info.available_bikes;
let statusStation = info.status;
// Affichage du rendu avec la méthode "document.getElementById" qui permet le renvoi d'un élément
document.getElementById("info-station").style.display = "block";// Apparition du bloc contenant les infos de la station sélectionnée
document.getElementById("adresse-station").innerText = address;
document.getElementById("place-libre").innerText = bikeStands;
document.getElementById("velo-dispo").innerText = availableBikes;
document.getElementById("etat-station").innerText = statusStation;
}
}; |
Partager