Document Maps JavaScript API Advanced Tutorial Geometric Calculation Distance, length, area

Distance, length, area

JS API provides a library for spatial data calculations, AMap.GeometryUtil, which can help calculate distance, length, area, etc. of points, lines, and surfaces.

This article introduces common mathematical calculation methods, including:

Calculate the actual distance between two points

AMap.GeometryUtil.distance

Calculate the shortest distance from a point to a line segment

AMap.GeometryUtil.distanceToSegment

Calculate the Shortest Distance from a Point to a Path

AMap.GeometryUtil.distanceToLine

Calculate the Actual Length of a Path

AMap.GeometryUtil.distanceOfLine

Calculate the Area of a Closed Region

AMap.GeometryUtil.ringArea

Example of Calculating Distance Between Two Points

1、User Manual

1.1 Calculate the actual distance between two points

When you need to calculate the actual ground distance between two points, you can use the static method AMap.GeometryUtil.distance, and the returned data is in meters.

var p1 = [100.479406, 13.752585];
var p2 = [100.483354, 13.744081];
//Return the ground distance between p1 and p2, unit: meters
var dis = AMap.GeometryUtil.distance(p1, p2);

1.2 Calculate the shortest distance from a point to a line segment

When calculating the shortest ground distance from a point to a line segment, you can use the static method AMap.GeometryUtil.distanceToSegment, which returns the data in meters.

var p0 = [100.481208, 13.75246];
var p1 = [100.477646, 13.748834];
var p2 = [100.485843, 13.746708];
//Returns the shortest ground distance from p0 to the line segment p1-p2, in meters
var dis = AMap.GeometryUtil.distanceToSegment(p0, p1, p2);

1.3 Calculate the shortest distance from a point to a path

When calculating the shortest ground distance from a point to a path, you can use the static method AMap.GeometryUtil.distanceToLine, which returns the data in meters. The difference between this method and the previous one is that this method supports line segments composed of multiple points.

var p0 = [100.478676, 13.756337];
var p1 = [100.475071, 13.753878];
var p2 = [100.479406, 13.753961];
var p3 = [100.483483, 13.753377];
//Returns the shortest ground distance from p0 to the line segment p1-p2-p3, unit: meters
var dis = AMap.GeometryUtil.distanceToSegment(p0, [p1, p2, p3]);

1.4 Calculate the actual length of the path

When you need to calculate the actual length of a certain path, you can use the static method AMap.GeometryUtil.distanceOfLine, and the returned data is in meters.

var p0 = [100.498374, 13.753294];
var p1 = [100.497645, 13.75146];
var p2 = [100.500263, 13.751752];
//Returns the actual length of the line segment p0-p1-p2, unit: meters
var dis = AMap.GeometryUtil.distanceOfLine([p0, p1, p2]);

1.5 Calculate the area of the closed region

When you need to calculate the area of the closed region formed by a specified path, you can use the static method AMap.GeometryUtil.ringArea, and the returned data is in square meters.

var p0 = [100.483783, 13.755545];
var p1 = [100.481337, 13.751418];
var p2 = [100.485929, 13.751501];
//Returns the area of the closed region formed by points p0-p1-p2, unit: square meters
var area = AMap.GeometryUtil.ringArea([p0, p1, p2]);