LngLatBounds
A LngLatBounds
object represents a geographical bounding box,
defined by its southwest and northeast points in longitude and latitude.
If no arguments are provided to the constructor, a null
bounding box is created.
Note that any Mapbox GL method that accepts a LngLatBounds
object as an argument or option
can also accept an Array
of two LngLatLike constructs and will perform an implicit conversion.
This flexible type is documented as LngLatBoundsLike.
Example
let sw = new LngLat(-73.9876, 40.7661);
let ne = new LngLat(-73.9397, 40.8002);
let llb = new LngLatBounds(sw, ne);
Constructors
new LngLatBounds()
new LngLatBounds(
sw
?: [number
,number
,number
,number
] |LngLatLike
| [LngLatLike
,LngLatLike
],ne
?:LngLatLike
):LngLatBounds
Parameters
Parameter | Type | Description |
---|---|---|
sw ? |
[number , number , number , number ] | LngLatLike | [LngLatLike , LngLatLike ] |
The southwest corner of the bounding box. OR array of 4 numbers in the order of west, south, east, north OR array of 2 LngLatLike: [sw,ne] |
ne ? |
LngLatLike |
The northeast corner of the bounding box. |
Returns
Example
let sw = new LngLat(-73.9876, 40.7661);
let ne = new LngLat(-73.9397, 40.8002);
let llb = new LngLatBounds(sw, ne);
Defined in
Methods
adjustAntiMeridian()
adjustAntiMeridian():
LngLatBounds
Adjusts the given bounds to handle the case where the bounds cross the 180th meridian (antimeridian).
Returns
The adjusted LngLatBounds
Example
let bounds = new LngLatBounds([175.813127, -20.157768], [-178. 340903, -15.449124]);
let adjustedBounds = bounds.adjustAntiMeridian();
// adjustedBounds will be: [[175.813127, -20.157768], [181.659097, -15.449124]]
Defined in
contains()
contains(
lnglat
:LngLatLike
):boolean
Check if the point is within the bounding box.
Parameters
Parameter | Type | Description |
---|---|---|
lnglat |
LngLatLike |
geographic point to check against. |
Returns
boolean
true
if the point is within the bounding box.
Example
let llb = new LngLatBounds(
new LngLat(-73.9876, 40.7661),
new LngLat(-73.9397, 40.8002)
);
let ll = new LngLat(-73.9567, 40.7789);
console.log(llb.contains(ll)); // = true
Defined in
extend()
extend(
obj
:LngLatLike
|LngLatBoundsLike
):this
Extend the bounds to include a given LngLatLike or LngLatBoundsLike.
Parameters
Parameter | Type | Description |
---|---|---|
obj |
LngLatLike | LngLatBoundsLike |
object to extend to |
Returns
this
Defined in
getCenter()
getCenter():
LngLat
Returns the geographical coordinate equidistant from the bounding box's corners.
Returns
The bounding box's center.
Example
let llb = new LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.getCenter(); // = LngLat {lng: -73.96365, lat: 40.78315}
Defined in
getEast()
getEast():
number
Returns the east edge of the bounding box.
Returns
number
The east edge of the bounding box.
Defined in
getNorth()
getNorth():
number
Returns the north edge of the bounding box.
Returns
number
The north edge of the bounding box.
Defined in
getNorthEast()
getNorthEast():
LngLat
Returns the northeast corner of the bounding box.
Returns
The northeast corner of the bounding box.
Defined in
getNorthWest()
getNorthWest():
LngLat
Returns the northwest corner of the bounding box.
Returns
The northwest corner of the bounding box.
Defined in
getSouth()
getSouth():
number
Returns the south edge of the bounding box.
Returns
number
The south edge of the bounding box.
Defined in
getSouthEast()
getSouthEast():
LngLat
Returns the southeast corner of the bounding box.
Returns
The southeast corner of the bounding box.
Defined in
getSouthWest()
getSouthWest():
LngLat
Returns the southwest corner of the bounding box.
Returns
The southwest corner of the bounding box.
Defined in
getWest()
getWest():
number
Returns the west edge of the bounding box.
Returns
number
The west edge of the bounding box.
Defined in
isEmpty()
isEmpty():
boolean
Check if the bounding box is an empty/null
-type box.
Returns
boolean
True if bounds have been defined, otherwise false.
Defined in
setNorthEast()
setNorthEast(
ne
:LngLatLike
):this
Set the northeast corner of the bounding box
Parameters
Parameter | Type | Description |
---|---|---|
ne |
LngLatLike |
a LngLatLike object describing the northeast corner of the bounding box. |
Returns
this
Defined in
setSouthWest()
setSouthWest(
sw
:LngLatLike
):this
Set the southwest corner of the bounding box
Parameters
Parameter | Type | Description |
---|---|---|
sw |
LngLatLike |
a LngLatLike object describing the southwest corner of the bounding box. |
Returns
this
Defined in
toArray()
toArray(): [
number
,number
][]
Returns the bounding box represented as an array.
Returns
[number
, number
][]
The bounding box represented as an array, consisting of the southwest and northeast coordinates of the bounding represented as arrays of numbers.
Example
let llb = new LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.toArray(); // = [[-73.9876, 40.7661], [-73.9397, 40.8002]]
Defined in
toString()
toString():
string
Return the bounding box represented as a string.
Returns
string
The bounding box represents as a string of the format
'LngLatBounds(LngLat(lng, lat), LngLat(lng, lat))'
.
Example
let llb = new LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
llb.toString(); // = "LngLatBounds(LngLat(-73.9876, 40.7661), LngLat(-73.9397, 40.8002))"
Defined in
convert()
static
convert(input
:LngLatBoundsLike
):LngLatBounds
Converts an array to a LngLatBounds
object.
If a LngLatBounds
object is passed in, the function returns it unchanged.
Internally, the function calls LngLat#convert
to convert arrays to LngLat
values.
Parameters
Parameter | Type | Description |
---|---|---|
input |
LngLatBoundsLike |
An array of two coordinates to convert, or a LngLatBounds object to return. |
Returns
A new LngLatBounds
object, if a conversion occurred, or the original LngLatBounds
object.
Example
let arr = [[-73.9876, 40.7661], [-73.9397, 40.8002]];
let llb = LngLatBounds.convert(arr); // = LngLatBounds {_sw: LngLat {lng: -73.9876, lat: 40.7661}, _ne: LngLat {lng: -73.9397, lat: 40.8002}}
Defined in
fromLngLat()
static
fromLngLat(center
:LngLat
,radius
:number
):LngLatBounds
Returns a LngLatBounds
from the coordinates extended by a given radius
. The returned LngLatBounds
completely contains the radius
.
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
center |
LngLat |
undefined |
center coordinates of the new bounds. |
radius |
number |
0 |
Distance in meters from the coordinates to extend the bounds. |
Returns
A new LngLatBounds
object representing the coordinates extended by the radius
.
Example
let center = new LngLat(-73.9749, 40.7736);
LngLatBounds.fromLngLat(100).toArray(); // = [[-73.97501862141328, 40.77351016847229], [-73.97478137858673, 40.77368983152771]]