需求:地图上显示某区域人口分布情况,并在圆圈上显示每个地点某类的人口总数(效果同:聚合图)。
效果图:
实现步骤
1.引入文件
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.13&key=高德申请的key值&plugin=AMap.Geocoder"></script>
2.创建一张地图
var map = new AMap.Map("container", {
center: [114.879026,25.768318],
resizeEnable: true,
});
var infoWindow = new AMap.InfoWindow({
anchor: 'middle-left',
});
3.和后端交互
function pregnantPeopleInfo() {
map.remove(single);
$.ajax({
type: 'get',
dataType: 'text',
url: '/pregnant/pregnant/queryPregnantCount',
p: [],
cache: false,
async: true,
success: function (p) {
var p = eval('(' + p + ')');
for (var i = 0; i < p.rows.length; i++) {
var div = document.createElement('div');
var htmlData='<span style=" opacity: 0.7;border-radius: 80%; height: 30px; width: 30px; display: inline-block; background: #238ff9; vertical-align: top;">\n' +
' <span style="display: block; color: #FFFFFF; height: 30px; line-height: 30px; text-align: center">'+p.rows[i].sqCommunitymanage.count+'人</span>\n' +
' </span>';
div.innerHTML=htmlData;
var point=[p.rows[i].sqCommunitymanage.longitude,p.rows[i].sqCommunitymanage.latitude];
var marker = new AMap.Marker({
position: point,
map: map,
title: p.rows[i].sqCommunitymanage.communityname,
visible: true,
content: div
});
single.push(marker);
marker.communityname = p.rows[i].sqCommunitymanage.communityname;
marker.count = p.rows[i].sqCommunitymanage.count;
marker.on('click', function(p){
infoWindow.setContent("<strong><span>"+p.target.communityname+"</span></strong><ul class='main'><li> 社区名称:<span style='color:blue'>"+p.target.communityname+"</span></li>"
+ "<li>怀孕总人数: <span style='color:blue'>"+p.target.count+" </span></li></ul>");
infoWindow.open(map, p.lnglat);
});
}
}
});
}
总结:开始用高德聚合图做的,但是没用实现在圆圈上显示人口数量。最终用创建marker+css的样式实现,和高德聚合图一样的效果。如果有建议、疑问或者更好的方案,欢迎大家留言。