步骤1. 需要判断浏览器是否支持绘制海量点
if (document.createElement('canvas').getContext) {
var p =new BMap.Point(yljgpoints[i]['lnt'],yljgpoints[i]['lat']);
p.id =yljgpoints[i]['id'];
points.push(p);
} else {
layer.msg("所有搜索结果的位置显示需要在chrome、safari、IE8+以上浏览器查看");
}
步骤2. 添加海量点到地图上
var pointCollectionYl = null;
var options = {
size: BMAP_POINT_SIZE_SMALL,
shape: BMAP_POINT_SHAPE_RHOMBUS,
color: '#ee0c0c'
};
map.removeOverlay(pointCollectionYl);
pointCollectionYl = new BMap.PointCollection(points, options);
map.addOverlay(pointCollectionYl);
pointCollectionYl.addEventListener('click', function (e) {
});
3. 示例生成海量点的完整代码
if (document.createElement('canvas').getContext)
{
map.clearOverlays();
for (var i = 0; i < 1000; i++) {
var new_point = new BMap.Point(a + getRandom(), b + getRandom());
points.push(new_point);
}
var options = {
size: BMAP_POINT_SIZE_SMALL,
shape: BMAP_POINT_SHAPE_STAR,
color: '#d340c3'
}
var pointCollection = new BMap.PointCollection(points, options);
pointCollection.addEventListener('click', function (e) {
alert('单击点的坐标为:' + e.point.lng + ',' + e.point.lat);
});
map.addOverlay(pointCollection);
} else {
alert('请在chrome、safari、IE8+以上浏览器查看本示例');
}
function getRandom() {
return Math.random() / 2;
}
4. 给覆盖物添加id
var yljgPointIcon = new BMap.Icon("${rc.contextPath}/static/image/icon-lb" + (yljgpoints[i].lx) + ".png", new BMap.Size(35, 35), {
imageSize: new BMap.Size(35, 35)
});
var cityPointmarker = new BMap.Marker(new BMap.Point(yljgpoints[i]['lnt'], yljgpoints[i]['lat']), {
icon: yljgPointIcon,
offset: new BMap.Size(9, -17),
title: yljgpoints[i]["dept_name"],
});
cityPointmarker.id = yljgpoints[i]['id'];
map.addOverlay(cityPointmarker);
cityPointmarker.addEventListener('click', function (e) {
console.log(e.target.id)
}