MapContext.calculateDistance
Calculate the length of a path that is defined by a series of coordinate points, and calculate the coordinates of a target point on the path at a specified distance from the starting point.
Parameters
Property | Type | Required | Description |
points | Array<Object> | Yes | An array of coordinate points on a path. The first element in the array is the starting point. Others are the target points. Example: copy
|
targetDistances | Array<Number> | No | An array of distances to a target point, which is named as "target distance". If this parameter is provided, this API calculates the coordinates of a target point on the path at the given target distance. Then it return the coordinates of a target point to the targetPoints array in the Example: copy
|
exportTotalDistance | Boolean | No | Whether to calculate the total length of a path. Default value: true |
success | Function | No | The callback function that is called upon successful call. |
fail | Function | No | The callback function that is called upon failed call. |
complete | Function | No | The callback function that is called upon call completion (to be executed upon either call success or failure). |
success
callback function
The parameters are in object type and have the following properties:
Property | Type | Required | Description |
distance | Number | No | The total length of a path that is defined by a series of coordinate points (see the points parameter). The total length is calculated by incrementally summing the straight-line distances between adjacent points. Note: If the exportTotalDistance parameter value is Unit: meter |
targetPoints | Array<Object> | No | The coordinates of a target point that are calculated based on the targetDistances parameter. For more, see the following targetPoint object. |
targetPoint object
Property | Type | Required | Description |
latitude | Number | Yes | The latitude of a target point on the path. |
longitude | Number | Yes | The longitude of a target point on the path. |
targetDistance | Number | Yes | The distance from the starting point to the specified target point. If the input targetPoints parameter is not provided or is empty, the targetDistances parameter is empty as well. Unit: meter |
index | Number | Yes | For the specified target point, this parameter indicates the index of the corresponding element in the input targetDistances parameter (array). The target points are sorted in ascending order based on the path length from the starting point. The order may not be the same as that of elements in the targetDistances parameter (array) . |
targetLineIndex | Number | Yes | The index of the line segment where this target point is located. The endpoints of the i-th line segment are |
Sample code
Page({
data: {
mapCtx: null,
},
onReady() {
this.data.mapCtx = my.createMapContext("map");
},
screenToMap() {
const aniPoints = [
{
latitude: 30.261775,
longitude: 120.102507,
},
{
latitude: 30.262794,
longitude: 120.103816,
},
{
latitude: 30.264036,
longitude: 120.10491,
},
{
latitude: 30.265194,
longitude: 120.10609,
},
{
latitude: 30.265824,
longitude: 120.107217,
},
{
latitude: 30.267446,
longitude: 120.109749,
},
{
latitude: 30.268715,
longitude: 120.112721,
},
];
this.data.mapCtx.calculateDistance({
points: aniPoints,
targetDistances: [100, 200, 300, 600],
success: res => {
console.log(res)
},
fail: error =>{
console.log(error)
}
});
},
});