百度地图获取可视区域及回调事件的用法简介

记录使用百度地图获取可视区域、回调事件的用法。

可视区域

使用百度地图获取可视区域的坐标点的方法:

原理:通过地图状态方法的接口getBounds()可获取到Bounds对象,包含东北和西南角的坐标点。

1
2
3
4
5
6
7
8
9
10
11
12
13
// 获取地图可视化区域的坐标点
function getViewAreaPoint(map) {
var bounds = map.getBounds();
console.log(bounds);
var swPoint = bounds.getSouthWest();
var nePoint = bounds.getNorthEast();
var nwPoint = new BMap.Point(swPoint.lng, nePoint.lat);
var sePoint = new BMap.Point(nePoint.lng, swPoint.lat);
console.log(swPoint);
console.log(nePoint);
console.log(nwPoint);
console.log(sePoint);
}

事件监听

原理:定义事件回调函数,添加事件的监听。

以click事件为例:

1
2
3
4
5
6
7
8
9
10
11
map.addEventListener("click", showInfo);

function showInfo(e) {
console.debug(e);
console.debug(e.type);
console.debug(e.target);
console.debug(e.point);
console.debug(e.pixel);
console.debug(e.overlay);
// console.debug(e.point.lng + ", " + e.point.lat);
}

百度地图的JavaScript类参考中列举click事件的说明:
事件: click
参数:(type, target, point, pixel, overlay)
描述:左键单击地图时触发此事件。

参数的说明:
type: 事件类型(“onclick”)
target: 地图实例
point: 经纬度坐标点Point对象
pixel: 像素坐标
overlay: 覆盖物对象

【以上】