如何访问$ .each循环外的appraisalAddress,以便在showMap函数中可用?这可能吗?我在appery.io服务执行中运行此代码,其中顶层的“数据”是对象数组
L.mapbox.accessToken = 'xyz';
var map = L.mapbox.map('map', 'mapbox.streets')
.setView([32.7833333, -96.8], 13);
function geocodeAppraisals() {
var appraisalAddress = 'Dallas';
$.each(data, function(i, item) {
appraisalAddress = item.PropertyAddress + " " + item.PropertyCity + " " + item.PropertyState + " " + item.PropertyZipCode;
console.log(appraisalAddress);
var geocoder = L.mapbox.geocoder('mapbox.places-v1');
geocoder.query(appraisalAddress, showMap);
});
function showMap(err, data) {
console.log('showMap');
console.log('appraisalAddress', appraisalAddress); // always shows the same address... the last item in $.each
if (err) throw err;
if (data.lbounds) {
map.fitBounds(data.lbounds);
} else if (data.latlng) {
var marker = L.marker([data.latlng[0], data.latlng[1]], {
icon: L.mapbox.marker.icon({
'marker-size': 'small',
//'marker-symbol': 'bus',
'marker-color': '#fa0'
}),
clickable: true,
draggable: false,
keyboard: true,
title: appraisalAddress,
alt: appraisalAddress,
zIndexOffset: 0,
opacity: 1.0,
riseOnHover: false,
riseOffset: 250
}).addTo(map);
map.setView([data.latlng[0], data.latlng[1]], 13);
}
}
}
geocodeAppraisals();
答案 0 :(得分:0)
看起来appraisalAddress
的范围是geocodeAppraisals
函数,这就是函数showMap
无法达到其值的原因。
您是否尝试在函数体外定义appraisalAddress
?这应该可以用于你的所有功能。