0
我的函数showMap
的参数数据似乎不起作用。我有undefined
为data.latlng
& data.lbounds
。我不知道如何调试。我的代码以前工作得很好...Uncaught TypeError:无法通过MapBox读取未定义的属性'0'
感谢您的帮助。
window.setTimeout(initMap, 100);
function initMap() {
//this should check if your leaflet is available or wait if not.
if(typeof L === "undefined"){
window.setTimeout(initMap, 100);
return;
}
L.mapbox.accessToken = 'myAccessToken';
$('.map').each(function() {
var map = L.mapbox.map($(this).attr('id'), 'mapbox.streets', {zoomControl:false}).setView([48.855, 2.4], 8);
var geocoder = L.mapbox.geocoder('mapbox.places');
var adress = document.getElementById($(this).attr('id'));
geocoder.query(adress.getAttribute("data-adress-map"), showMap);
function showMap(err, data) {
// The geocoder can return an area, like a city, or a
// point, like an address. Here we handle both cases,
// by fitting the map bounds to an area or zooming to a point.
if (data.lbounds) {
map.fitBounds(data.lbounds);
}
else if (data.latlng) {
map.setView([data.latlng[0], data.latlng[1]], 13);
}
var marker = L.marker([data.latlng[0], data.latlng[1]]).addTo(map);
marker.bindPopup(adress.getAttribute("data-title-map") +"<br>"+ adress.getAttribute("data-adress-map"));
new L.Control.Zoom({ position: 'bottomright' }).addTo(map);
}
});
}
编辑:
我发现。这是一个非常愚蠢的问题。 data
工作正常。事实上,这是我的地址不起作用的原因内部的逗号。非常感谢。
数据包含什么? – depperm
如果您找到答案,请关闭此问题。 – tmcw