我制作了一个地图,并使用标记添加了单击事件,但是单击标记时显示“无法读取未定义的属性'number'”。可能是因为它无法识别[this.i],但是[this.i]却在上面创建了所有标记。代码:
class carte {
constructor(donnee) {
this.map = L.map('map').setView([45.764043, 4.835659], 12);
this.nosDonnee = donnee;
this.i = 0;
this.calque = L.tileLayer('https://{s}.tile.openstreetmap.se/hydda/full/{z}/{x}/{y}.png', {
maxZoom: 20
}).addTo(this.map);
this.marker = L.marker([(parseFloat(this.nosDonnee[this.i].position.lat)), (parseFloat(this.nosDonnee[this.i].position.lng))]).addTo(this.map);
this.addMarker();
}
addMarker() {
for (this.i; this.i < this.nosDonnee.length; this.i++) {
this.marker = L.marker([(parseFloat(this.nosDonnee[this.i].position.lat)), (parseFloat(this.nosDonnee[this.i].position.lng))]).addTo(this.map);
this.marker.addEventListener('click', () => {
document.getElementById('statusDonnees').innerHTML = this.nosDonnee[this.i].number;
})
}
}
}
function ajaxget() {
let url = new XMLHttpRequest();
url.open('GET', 'https://api.jcdecaux.com/vls/v1/stations?contract=lyon&apiKey=69b9e30a10d230227d189617d1827d3586cf19be');
url.onload = function() {
let nouvCarte = new carte(JSON.parse(url.responseText));
}
url.send();
}
ajaxget();