mapcontext.calculatedistance(object object)
基础库 或更高版本; 且 支付宝客户端 10.1.90 或更高版本; 若版本较低,建议采取 兼容处理 。
支小宝客户端 支持
安诊儿客户端 支持
小程序开发者工具: 支持。
主体: 企业支付宝小程序 、 个人支付宝小程序
计算由一系列坐标点所定义的路径的长度,并可计算出该路径上距起始点指定距离的点的坐标。
例如:入参 { points: [a, b, c],targetdistances: [d1, d2] },success 回调的参数 { distance, targetpoints: [x, y] }
用 dist(m, n) 代表坐标 m 到 n 的直线距离,则有 distance == dist(a, b) dist(b, c)
若 0 <= d1 < dist(a, b),则 x 在线段 ab 上(x.targetlineindex == 0),且 dist(a, x) == d1
若 0 <= d2 - dist(a, b) < dist(b, c),则 y 在线段 bc 上(y.targetlineindex == 1),且 dist(a, b) dist(b, y) == d2
object object
查看示例
属性 | 类型 | 默认值 | 必填 | 描述 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
points | array | - | 是 | 顺序排列的路径节点数组,第一个点为起点 |
||||||||||
|
||||||||||||||
exporttotaldistance | boolean | true | 否 | 是否需要计算路径总长度 |
||||||||||
targetdistances | array |
- | 否 | 目标距离数组。如提供,接口将按其中每个目标距离计算出路径上的目标点,放到出参的 targetpoints 数组 |
||||||||||
success | function | - | 否 | 调用成功的回调函数 |
||||||||||
fail | function | - | 否 | 调用失败的回调函数 |
||||||||||
complete | function | - | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
success 回调函数
参数
object res
查看示例
属性 | 类型 | 描述 | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
distance | number | 由入参 points 中的点所定义的路径总长度(相邻点的直线距离逐段累加)。单位:米。如果传入的 exporttotaldistance 为 false,则不返回 distance |
|||||||||||||||||||
targetpoints | array | 按入参 targetdistances 所计算出的目标点 |
|||||||||||||||||||
|
<view>
<map id="map" style="width:100%; height:500px"/>
<button ontap="calculatedistance">计算由一系列坐标点所定义的路径的长度button>
view>
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)
}
});
},
});
{
"distance": 0,
"success": true,
"targetpoints": [
{
"index": 0,
"latitude": 30.261775,
"longitude": 120.102507,
"targetdistance": 100,
"targetlineindex": 0
},
{
"index": 1,
"latitude": 30.261775,
"longitude": 120.102507,
"targetdistance": 200,
"targetlineindex": 0
},
{
"index": 2,
"latitude": 30.261775,
"longitude": 120.102507,
"targetdistance": 300,
"targetlineindex": 0
},
{
"index": 3,
"latitude": 30.261775,
"longitude": 120.102507,
"targetdistance": 600,
"targetlineindex": 0
}
]
}