参考地址 百度地图多边形消失的问题解决
产生问题的原因是因为我们用的普通的点数组生成的多边形,我们应该用百度的点数组生成就没问题了。
具体啥原因,咱不知道,也不敢问啊。
问题描述:
地图移动或者放大地图时,如果多边形不能全部可见则会消失
问题代码:
var points = [{"lng":116.4935302734375,"lat":40.0506591796875},{"lng":116.4935302734375,"lat":40.05615234375},{"lng":116.488037109375,"lat":40.05615234375},{"lng":116.488037109375,"lat":40.0616455078125},{"lng":116.488037109375,"lat":40.067138671875},{"lng":116.488037109375,"lat":40.0726318359375},{"lng":116.4935302734375,"lat":40.0726318359375}];
// points参数没有通过new BMap.Point()构造对象,就会出现上面的问题
var ply = new BMap.Polygon(points, {strokeWeight: 2, strokeColor: '#0905ff', fillColor: '#5182E4', fillOpacity: 0.2}); // 建立多边形覆盖物
map.addOverlay(ply); // 添加覆盖物
正确代码:
var points = [{"lng":116.4935302734375,"lat":40.0506591796875},{"lng":116.4935302734375,"lat":40.05615234375},{"lng":116.488037109375,"lat":40.05615234375},{"lng":116.488037109375,"lat":40.0616455078125},{"lng":116.488037109375,"lat":40.067138671875},{"lng":116.488037109375,"lat":40.0726318359375},{"lng":116.4935302734375,"lat":40.0726318359375}];
// 正确代码段
var ps = []
for (var i in points) {
ps.push(new BMap.Point(points[i].lng,points[i].lat));
}
var ply = new BMap.Polygon(ps, {strokeWeight: 2, strokeColor: '#0905ff', fillColor: '#5182E4', fillOpacity: 0.2}); // 建立多边形覆盖物
map.addOverlay(ply); // 添加覆盖物