Skip to content

LngLatBounds


LngLatBounds 对象表示一个地理位置矩形区域,由西南和东北两个方位的位置点组成。
如果未提供参数,将返回null

js
new DiMap.LngLatBounds(lng?: LngLatLike, lat?: LngLatLike)

示例

js
const sw = new DiMap.LngLat(-73.9876, 40.7661);
const ne = new DiMap.LngLat(-73.9397, 40.8002);
const llb = new DiMap.LngLatBounds(sw, ne);

实例属性

静态方法

1. convert(lll: [sw: LngLatBoundsLike): LngLatBounds

将经纬度字面量表示法转换为LngLatBounds对象

js
const arr = [[-73.9876, 40.7661], [-73.9397, 40.8002]];
const llb = DiMap.LngLatBounds.convert(arr);
console.log(llb);   // = LngLatBounds {_sw: LngLat {lng: -73.9876, lat: 40.7661}, _ne: LngLat {lng: -73.9397, lat: 40.8002}}

实例方法

1. setNorthEast(ne: LngLatLike): LngLatBounds

设置东北角坐标

示例

js
const sw = new DiMap.LngLat(-73.9876, 40.7661);
const ne = new DiMap.LngLat(-73.9397, 40.8002);
const llb = new DiMap.LngLatBounds(sw, ne);
llb.setNorthEast([-73.9397, 42.8002]);

2. setSouthWest(sw: LngLatLike): LngLatBounds

设置西南角坐标

示例

js
const sw = new DiMap.LngLat(-73.9876, 40.7661);
const ne = new DiMap.LngLat(-73.9397, 40.8002);
const llb = new DiMap.LngLatBounds(sw, ne);
llb.setSouthWest([-73.9876, 40.2661]);

3. extend(obj: LngLatLike | LngLatBoundsLike): LngLatBounds

给定位置或已有区域,扩展返回新区域。对于计算polyline的bounds很有用

示例

js
const sw = new DiMap.LngLat(-73.9876, 40.7661);
const ne = new DiMap.LngLat(-73.9397, 40.8002);
const llb = new DiMap.LngLatBounds(sw, ne);
llb.extend([-72.9876, 42.2661]);

4. getCenter(): LngLat

返回与边界框角等距的地理坐标(矩形中心)

示例

js
const llb = new DiMap.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.getCenter(); // = LngLat {lng: -73.96365, lat: 40.78315}

5. getSouthWest(): LngLat

返回西南角坐标

示例

js
const llb = new DiMap.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.getSouthWest(); // LngLat {lng: -73.9876, lat: 40.7661}

6. getNorthEast(): LngLat

返回东北角坐标

7. getNorthWest(): LngLat

返回西北角坐标

8. getSouthEast(): LngLat

返回东南角坐标

9. getWest(): LngLat

返回正西方向坐标

10. getSouth(): LngLat

返回正南方向坐标

11. getEast(): LngLat

返回正东方向坐标

12. getNorth(): LngLat

返回正北方向坐标

2. toArray(): [number, number][]

返回一个由经纬度数字组成的数组

示例

js
const llb = new DiMap.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.toArray(); // = [[-73.9876, 40.7661], [-73.9397, 40.8002]]

3. toString(): string

返回字符串表示形式: 'LngLatBounds(LngLat(lng, lat), LngLat(lng, lat))'

示例

js
const llb = new DiMap.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.toString(); // = "LngLatBounds(LngLat(-73.9876, 40.7661), LngLat(-73.9397, 40.8002))"

4. isEmpty(): boolean

判断区域是否为空

示例

js
const llb = new DiMap.LngLatBounds();
llb.isEmpty(); // true
llb.setNorthEast([-73.9876, 40.7661]);
llb.setSouthWest([-73.9397, 40.8002]);
llb.isEmpty(); // false

5. contains(ll: LngLatLike): boolean

判断坐标是否在区域内

示例

js
const llb = new DiMap.LngLatBounds(
  new DiMap.LngLat(-73.9876, 40.7661),
  new DiMap.LngLat(-73.9397, 40.8002)
);

const ll = new DiMap.LngLat(-73.9567, 40.7789);

console.log(llb.contains(ll)); // = true