把后台数据调过来然后for循环创建marker标记,然后每个标记创建一个信息窗口绑定,点击的时候出现一个信息窗口然后里面显示出数据提供的内容和姓名,我做出来之后数据读不出来,不管点击任何个marker,信息都是第一次点击的内容
var newmarkers = [...];//provinces是后台调过来的数据
for (var i = 0; i <provinces[0]['subDistricts'].length; i += 1) {
//console.log(i);
var newlnglat = provinces[0]['subDistricts'][i]['center'].split(',');
var safelevel = provinces[0]['subDistricts'][i]['level'];
var webdress;
var nameId=provinces[0]["subDistricts"][i]["name"];
var contId=provinces[0]['subDistricts'][i]["intro"];
if (safelevel>=85) {
webdress='http://www.haotu.net/up/1155/16/152.png';
}else if(safelevel>=75&&safelevel<85){
webdress='http://www.haotu.net/up/1155/16/156.png';
}else if(safelevel==60){
webdress='http://www.haotu.net/up/1155/16/153.png';
}else{
webdress='http://www.haotu.net/up/1155/16/151.png'
}
var newmarker = new AMap.Marker({
position: newlnglat,
icon: webdress,
//offset: {x: -8,y: -34}
});
var infoWindow = new AMap.InfoWindow({
isCustom: true, //使用自定义窗体
content: '<p id="infowindow_detail" class="infowindow"><p><span>姓名:</span>'+nameId+'</p><p>'+contId+'</p></p>',
offset: new AMap.Pixel(16, -45)
});
AMap.event.addListener(map, 'zoomend', _clsoeInfoWindow);
function _clsoeInfoWindow(){
if(map.getZoom()<13){
//alert("1");
infoWindow.close();
};
}
//AMap.event.addListener(newmarker, 'click', function() {
// infoWindow.open(map, newmarker.getPosition());
// });
AMap.event.addListener(newmarker, 'click', function(e) {
//alert(e.lnglat)
//infoWindow.open(map, newmarker.getPosition());
infoWindow.open(map, e.lnglat);
});
newmarkers.push(newmarker);
};
// 添加点聚合
function addCluster() {
map.plugin(["AMap.MarkerClusterer"], function() {
var cluster = new AMap.MarkerClusterer(map, newmarkers);
});
}
addCluster();