Skip to content

Boundary

此类表示一个行政区域的边界。

构造函数

构造函数描述
Boundary()创建行政区域搜索的对象实例

方法

方法返回值描述
get(name: String, callback: function)null返回行政区域的边界。 name: 查询省、直辖市、地级市、或县的名称。 callback:执行查询后,数据返回到客户端的回调函数,数据以回调函数的参数形式返回。返回结果是一个数组,数据格式如下: arr[0] = "x1, y1; x2, y2; x3, y3; ..." arr[1] = "x1, y1; x2, y2; x3, y3; ..." arr[2] = "x1, y1; x2, y2; ..." … 否则回调函数的参数为null

示例

ts
// 创建行政区域边界实例
const boundary = new BMapGL.Boundary();

// 获取北京市的边界
boundary.get('北京市', (arr: string[]) => {
  if (arr) {
    console.log('边界点数量:', arr.length);
    
    // 遍历所有边界点
    arr.forEach((points, index) => {
      console.log(`第 ${index + 1} 个边界:`, points);
      
      // 将边界点转换为坐标数组
      const coordinates = points.split(';').map(point => {
        const [x, y] = point.split(',').map(Number);
        return new BMapGL.Point(x, y);
      });
      
      // 绘制边界
      const polygon = new BMapGL.Polygon(coordinates, {
        strokeColor: '#1869FF',
        strokeWeight: 2,
        strokeOpacity: 0.8,
        fillColor: '#1869FF',
        fillOpacity: 0.2
      });
      map.addOverlay(polygon);
    });
  } else {
    console.log('未找到边界数据');
  }
});

基于 MIT 许可发布