js遍历json求解。。。
var adds=[
{
Latitude: "39.95037",
Longitude: "116.488747",
LocationName: "北京市朝阳区农展馆北路78号",
},
{
Latitude: "39.97037",
Longitude: "116.498747",
LocationName: "78号",
},
{
Latitude: "39.93037",
Longitude: "116.478747",
LocationName: "北京市朝阳区",
},
]
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.498747, 39.97037), 15);
for(var i in adds)
{
var point = new BMap.Point(adds[i].Longitude, adds[i].Latitude);
var marker = new BMap.Marker(point);
var infoWindow = new BMap.InfoWindow(adds[i].LocationName);
marker.addEventListener("click", function(){
this.openInfoWindow(infoWindow);
});
map.addOverlay(marker);
}
执行点击事件时,每个元素的LocationName都是相同的,该怎么处理?
------解决方案--------------------修正,应如下:
for(var i in adds)
{
var point = new BMap.Point(adds[i].Longitude, adds[i].Latitude);
var marker = new BMap.Marker(point);
var infoWindow = new BMap.InfoWindow(adds[i].LocationName);
(function(infoWindow ){
marker.addEventListener("click", function(){
this.openInfoWindow(infoWindow);
});
})(infoWindow);
map.addOverlay(marker);
}