GeometryUtil
AMap.GeometryUtil
GeometryUtil is a library for spatial data calculations, added in v1.4.2. It supports spatial relationship calculations, length, area calculations, etc., for points, lines, and polygons.
Method
distance(p1, p2)
Calculate the actual distance between two latitude and longitude points. Unit: meters
Parameter:
p1 (LngLat) Latitude and Longitude Point 1
p2 (LngLat) Latitude and Longitude Point 2
return value: (number) The actual distance between two latitude and longitude points
Demo:
var p1 = [116.434027, 39.941037];
var p2 = [116.461665, 39.941564];
//Return the ground distance between p1 and p2, unit: meters
var dis = AMap.GeometryUtil.distance(p1, p2);ringArea(ring)
Calculate the actual area of a region enclosed by a latitude and longitude path. Unit: square meters
Parameter: ring (Array<LngLat>) Region enclosed by latitude and longitude path
return value: (number) Actual area of the region
Demo:
var path = [
[116.169465, 39.93267],
[116.16026, 39.924492],
[116.150625, 39.710019],
[116.183198, 39.70992],
];
AMap.GeometryUtil.ringArea(path);ringAreaByAltitude(AverageAltitude,number)
Calculate the actual area of a region enclosed by a latitude and longitude path with absolute altitude. Unit: square meters (Note: This method is applicable to JSAPI v2.1Beta and above)
Parameter:
AverageAltitude (Array<LngLat>) Region enclosed by latitude and longitude path
number(number) Absolute altitude
return value: (number) Actual area of the region
isClockwise(ring)
Determine if a latitude and longitude path is clockwise
Parameter: ring (Array<LngLat>) Latitude and longitude path
return value: (boolean) Clockwise: true, Counterclockwise: false
Demo:
var path = [
[116.403322, 39.920255],
[116.410703, 39.897555],
[116.402292, 39.892353],
[116.389846, 39.891365],
];
AMap.GeometryUtil.isClockwise(path);typePolygon(ring)
Determine the type of a latitude and longitude path surface
Parameter: ring (Array<LngLat>)
return value: boolean
makesureClockwise(ring)
Convert a path to clockwise
Parameter: ring (Array<LngLat>) Latitude and longitude path
return value: (Array<[number, number]>) Clockwise latitude and longitude path
Demo:
var path = [
[116.389846, 39.891365],
[116.402292, 39.892353],
[116.410703, 39.897555],
[116.403322, 39.920255],
];
AMap.GeometryUtil.makesureClockwise(path);makesureAntiClockwise(ring)
Convert a path to counterclockwise
Parameter: ring (Array<LngLat>) Latitude and longitude path
return value: (Array<[number, number]>) Counterclockwise latitude and longitude path
Demo:
var path = [
[116.403322, 39.920255],
[116.410703, 39.897555],
[116.402292, 39.892353],
[116.389846, 39.891365],
];
AMap.GeometryUtil.makesureAntiClockwise(path);distanceOfLine(ring)
Calculate the actual length of a latitude and longitude path. Unit: meters
Parameter: ring (Array<LngLat>) Latitude and longitude path
return value: (number) Actual length of the path
Demo:
var arr = [
new AMap.LngLat("116.368904", "39.913423"),
new AMap.LngLat("116.382122", "39.901176"),
new AMap.LngLat("116.387271", "39.912501"),
]; //Array of latitude and longitude coordinates
AMap.GeometryUtil.distanceOfLine(arr);distanceOfLineByAltitude(ring)
Calculate the actual length of a path with absolute elevation in a latitude and longitude zone. Unit: meters (Note: This method is applicable to JSAPI v2.1Beta and above)
Parameter: ring (Array<LngLat>) Latitude and longitude zone
return value: (number) Actual length
distanceLineByAltitude(pt1, pt2)
Calculate the actual length of the spatial distance between two points with absolute elevation in a latitude and longitude band. Unit: meters (Note: This method is applicable to JSAPI v2.1Beta and above)
Parameter:
pt1 (Array<number>) Latitude and longitude
pt2 (Array<number>) Latitude and longitude
return value: (number) Actual length
ringRingClip(ring1, ring2)
Calculate the intersection area of two latitude and longitude surfaces. Only applicable to convex polygons
Parameter:
ring1 (Array<LngLat>) Latitude and longitude surface
ring2 (Array<LngLat>) Latitude and longitude surface
return value: (Array<number>) Latitude and longitude path of the intersection area
Demo:
var path1 = [
[116.376907, 39.910967],
[116.384911, 39.919505],
[116.40109, 39.919728],
[116.411132, 39.911408],
[116.412076, 39.899135],
[116.402292, 39.892353],
];
var path2 = [
[116.390233, 39.906602],
[116.395254, 39.908668],
[116.400661, 39.906667],
[116.400962, 39.898589],
[116.395769, 39.894855],
[116.390147, 39.898049],
];
AMap.GeometryUtil.ringRingClip(path1, path2);doesSegmentsIntersect(p1, p2, p3, p4)
Determine if two line segments intersect
Parameter:
p1 (LngLat) Latitude and longitude
p2(LngLat) Latitude and longitude
p3(LngLat) Latitude and longitude
p4 (LngLat) Latitude and longitude
return value: (boolean) Intersection: true, no intersection: false
Demo:
var p1 = [116.376907, 39.910967];
var p2 = [116.384911, 39.919505];
var p3 = [116.390233, 39.906602];
var p4 = [116.395254, 39.908668];
AMap.GeometryUtil.doesSegmentsIntersect(p1, p2, p3, p4);doesSegmentLineIntersect(p1, p2, line)
Determine if a line segment intersects with a path
Parameter:
p1 (LngLat) Latitude and longitude
p2 (LngLat) Latitude and longitude
line (Array<LngLat>) Latitude and longitude path
return value: (boolean) Intersection: true, no intersection: false
Demo:
var p1 = [116.395521, 39.903693];
var p2 = [116.387643, 39.90799];
var line = [
[116.390233, 39.906602],
[116.395254, 39.908668],
[116.400661, 39.906667],
[116.400962, 39.898589],
];
AMap.GeometryUtil.doesSegmentLineIntersect(p1, p2, line);doesSegmentRingIntersect(p1, p2, ring)
Determine whether a line segment intersects with a ring
Parameter:
p1 (LngLat) Latitude and longitude
p2 (LngLat) Latitude and longitude
line (Array<LngLat>) Latitude and longitude surface
return value: (boolean) Intersection: true, no intersection: false
Demo:
var p1 = [116.171814, 39.90944];
var p2 = [116.084153, 39.95227];
var ring = [
[116.169465, 39.93267],
[116.16026, 39.924492],
[116.186138, 39.879817],
];
AMap.GeometryUtil.doesSegmentRingIntersect(p1, p2, ring);doesSegmentPolygonIntersect(p1, p2)
Determine if a line segment intersects with multiple rings
Parameter:
p1 (LngLat) Latitude and longitude
p2 (LngLat) Latitude and longitude
return value: (boolean) Intersection: true, no intersection: false
doesLineLineIntersect(line, line)
Determine if two latitude and longitude paths intersect
Parameter:
line (Array<LngLat>) Latitude and longitude path
line (Array<LngLat>) Latitude and longitude path
return value: (boolean) Intersection: true, no intersection: false
Demo:
var path1 = [
[116.376907, 39.910967],
[116.384911, 39.919505],
[116.40109, 39.919728],
];
var path2 = [
[116.390233, 39.906602],
[116.395254, 39.908668],
[116.400661, 39.906667],
];
AMap.GeometryUtil.doesLineLineIntersect(path1, path2);doesLineRingIntersect(line, ring)
Determine if a latitude and longitude path intersects with a latitude and longitude surface
Parameter:
line (Array<LngLat>) Latitude and longitude path
ring (Array<LngLat>) Latitude and longitude surface
return value: (boolean) Intersection: true, No intersection: false
Demo:
var line = [
[116.390233, 39.906602],
[116.395254, 39.908668],
[116.400661, 39.906667],
[116.400962, 39.898589],
[116.395769, 39.894855],
[116.390147, 39.898049],
];
var ring = [
[116.376907, 39.910967],
[116.384911, 39.919505],
[116.40109, 39.919728],
[116.411132, 39.911408],
[116.412076, 39.899135],
[116.402292, 39.892353],
];
AMap.GeometryUtil.doesLineRingIntersect(line, ring);doesRingRingIntersect(ring1, ring2)
Determine if two latitude and longitude surfaces intersect
Parameter:
ring1(Array<LngLat>) Latitude and longitude surface
ring2(Array<LngLat>) Latitude and longitude surface
return value: (boolean) Intersection: true, No intersection: false
Demo:
var ring1 = [
[116.390233, 39.906602],
[116.395254, 39.908668],
[116.400661, 39.906667],
[116.400962, 39.898589],
[116.395769, 39.894855],
[116.390147, 39.898049],
];
var ring2 = [
[116.376907, 39.910967],
[116.384911, 39.919505],
[116.40109, 39.919728],
[116.411132, 39.911408],
[116.412076, 39.899135],
[116.402292, 39.892353],
];
AMap.GeometryUtil.doesRingRingIntersect(ring1, ring2);pointInRing(p, ring)
Determine if a point is within the ring, supporting any coordinate system
Parameter:
p(LngLat) Latitude and longitude
ring (Array<LngLat>) Latitude and longitude surface
return value: (boolean) Is it within the ring
isPointInRing(p, ring)
Determine if a point is within the ring
Parameter:
p(LngLat) Latitude and longitude
ring (Array<LngLat>) Latitude and longitude surface
return value: (boolean) Is it within the ring
Demo:
var p = [116.566298, 40.014179];
var ring = [
[116.169465, 39.93267],
[116.16026, 39.924492],
[116.186138, 39.879817],
[116.150625, 39.710019],
];
AMap.GeometryUtil.isPointInRing(p, ring);isPointInBbox(p, bbox)
Determine if a point is within the bounding box
Parameter:
p(LngLat) Latitude and longitude
ring (Bounds) Latitude and longitude rectangular range
return value: (boolean) Whether it is within the bounding box
Demo:
var p = [116.566298, 40.014179];
//Set the bounding box range, defined by the latitude and longitude coordinates of the southwest and northeast corners
var southWest = new AMap.LngLat(116.356449, 39.859008); //Latitude and longitude coordinates of the southwest corner
var northEast = new AMap.LngLat(116.417901, 39.893797); //Latitude and longitude coordinates of the northeast corner
var ring = new AMap.Bounds(southWest, northEast); //Create a latitude and longitude bounding box for a feature object
AMap.GeometryUtil.isPointInBbox(p, ring);isRingInRing(ring1, ring2)
Determine whether a ring is within another ring
Parameter:
ring1(Array<LngLat>) Latitude and longitude surface
ring2 (Array<LngLat>) Latitude and longitude surface
return value: (boolean) Returns true if ring1 is within ring2, otherwise returns false
Demo:
var ring1 = [
[116.390233, 39.906602],
[116.395254, 39.908668],
[116.400661, 39.906667],
[116.400962, 39.898589],
[116.395769, 39.894855],
[116.390147, 39.898049],
];
var ring2 = [
[116.376907, 39.910967],
[116.384911, 39.919505],
[116.40109, 39.919728],
[116.411132, 39.911408],
[116.412076, 39.899135],
[116.402292, 39.892353],
[116.3874, 39.892518],
[116.376971, 39.899267],
];
AMap.GeometryUtil.isRingInRing(ring1, ring2);isPointInPolygon(p, rings)
Determine whether a point is within the area composed of multiple rings
Parameter:
p (LngLat) Latitude and longitude
rings ([Array<ringLngLat>]) A set of latitude and longitude surfaces
return value: (boolean) Whether within the area composed of multiple rings
Demo:
var p = [116.390756, 39.917331];
var ring1 = [
[116.376907, 39.910967],
[116.384911, 39.919505],
[116.40109, 39.919728],
];
var ring2 = [
[116.411132, 39.911408],
[116.412076, 39.899135],
[116.402292, 39.892353],
[116.3874, 39.892518],
[116.376971, 39.899267],
];
AMap.GeometryUtil.isPointInPolygon(p, [ring1, ring2]);isPointInPolygons(p, polygons)
Determine whether a point is inside a polygon with holes
Parameter:
p (LngLat) Latitude and longitude
polygons ([[Array<ringLngLat>]]) Polygon with holes
return value: (boolean) Whether inside a polygon with holes
Demo:
var p = [116.47032, 39.961578];
var polygons = [
[
[116.472833, 39.962855],
[116.471476, 39.963143],
[116.469709, 39.963297],
[116.46826, 39.961842],
[116.470652, 39.960202],
[116.474724, 39.960193],
[116.473896, 39.961207],
[116.473988, 39.962838],
],
[
[116.472867, 39.962644],
[116.472987, 39.962015],
[116.47198, 39.961771],
],
];
AMap.GeometryUtil.isPointInPolygons(p, polygons);isPointOnSegment(p1, p2, p3, tolerance)
Determine whether P1 is on the P2P3 line segment, with tolerance as the error range
Parameter:
p1(LngLat) Latitude and longitude
p2(LngLat) Latitude and longitude
p3 (LngLat) Latitude and longitude
tolerance (Number) Error range, unit: meters
return value: (boolean) On P2P3 true, off P2P3 false
Demo:
var p1 = [116.472833, 39.962855];
var p2 = [116.471476, 39.963143];
var p3 = [116.469709, 39.963297];
AMap.GeometryUtil.isPointOnSegment(p1, p2, p3, 5);isPointOnLine(p, line, tolerance)
Determine whether P is on the line, with tolerance as the error range
Parameter:
p (LngLat) Latitude and longitude
line (Array<LngLat>) Latitude and longitude path
tolerance (Number) Error range, unit: meters
return value: (boolean) On the line true, off the line false
Demo:
var p = [116.472833, 39.962855];
var line = [
[116.368904, 39.913423],
[116.382122, 39.901176],
[116.387271, 39.912501],
[116.398258, 39.9046],
];
AMap.GeometryUtil.isPointOnLine(p, line, 5);isPointOnRing(p, ring, tolerance)
Determine whether P is on the edge of the ring, with tolerance as the error range
Parameter:
p (LngLat) Latitude and longitude
ring(Array<LngLat>) Latitude and longitude surface
tolerance (Number) Tolerance, unit: meters
return value: (boolean) On the line edge true, not on the line edge false
Demo:
var p = [116.378232, 39.904782];
var line = [
[116.368904, 39.913423],
[116.382122, 39.901176],
[116.387271, 39.912501],
];
AMap.GeometryUtil.isPointOnRing(p, ring, 5);isPointOnPolygon(p,rings,tolerance)
Determine whether P is on the edge of multiple rings, with tolerance as the error margin
Parameter:
p (LngLat) Latitude and longitude
rings ([Array<ringLngLat>]) A set of latitude and longitude surfaces
tolerance (Number) Tolerance
return value: (boolean) True if on multiple line edges, false if not on multiple line edges
Demo:
var p = [116.378232, 39.904782];
var line1 = [
[116.169465, 39.93267],
[116.16026, 39.924492],
[116.186138, 39.879817],
];
var line2 = [
[116.150625, 39.710019],
[116.183198, 39.70992],
[116.22695, 39.777616],
];
AMap.GeometryUtil.isPointOnPolygon(p, [line1, line2], 5);closestOnSegment(p1, p2, p3)
Calculate the point on P2P3 closest to P1
Parameter:
p1 (LngLat) Latitude and longitude
p2 (LngLat) Latitude and longitude
p3 (LngLat) Latitude and longitude
return value: ([Number,Number]) Latitude and longitude of the nearest point
Demo:
var p = [116.566298, 40.014179];
var p2 = [116.169465, 39.93267];
var p3 = [116.16026, 39.924492];
AMap.GeometryUtil.closestOnSegment(p, p2, p3);
closestOnLine(p, line)
Calculate the point on the line closest to P
Parameter:
p (LngLat) Latitude and longitude
line(Array<LngLat>) Latitude and longitude path
return value: ([Number,Number]) Latitude and longitude of the nearest point
Demo:
var p = [116.378232, 39.904782];
var line = [
[116.169465, 39.93267],
[116.16026, 39.924492],
[116.186138, 39.879817],
];
AMap.GeometryUtil.closestOnLine(p, line);distanceToSegment(p1, p2, p3)
Calculate the distance from P2P3 to P1. Unit: meters
Parameter:
p1(LngLat) Latitude and longitude
p2(LngLat) Latitude and longitude
p3(LngLat) Latitude and longitude
return value: (Number) Distance, unit: meters
Demo:
var p = [116.566298, 40.014179];
var p2 = [116.169465, 39.93267];
var p3 = [116.16026, 39.924492];
AMap.GeometryUtil.distanceToSegment(p, p2, p3);distanceToLine(p, line)
Calculate the distance from P to line. Unit: meters
Parameter:
p(LngLat) Latitude and longitude
line(Array<LngLat>) Latitude and longitude path
return value: (Number) Distance, unit: meters
Demo:
var p = [116.378232, 39.904782];
var line = [
[116.169465, 39.93267],
[116.16026, 39.924492],
[116.186138, 39.879817],
];
AMap.GeometryUtil.distanceToLine(p, line);