turf.js 路径上两点之间的距离
Distance between two points over a path with turf.js
是否可以使用 turfjs 计算直线路径上两点之间的距离?怎么样?
使用 turf.lineSlice
使用你的两个点对线进行切片。这将 return 一条新线,其中仅包含两点之间线上的部分。接下来你可以使用turf.lineDistance
来计算那条切片线的距离。
var line = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[4.0155029296875, 40.18726672309203 ],
[4.0814208984375, 40.195659093364654 ],
[4.15283203125, 40.19146303804063 ],
[4.185791015625, 40.17887331434696 ],
[4.2242431640625, 40.15788524950653 ],
[4.273681640625, 40.111688665595956 ],
[4.3011474609375, 40.06125658140474 ],
[4.290161132812499, 40.02340800226773 ],
[4.3341064453125, 39.97291055131899 ],
[4.4000244140625, 39.96870074491696 ],
[4.4659423828125, 39.977120098439634 ],
[4.531860351562499, 40.002371935876475 ],
[4.5648193359375, 40.052847601823984 ],
[4.5648193359375, 40.11588965267845 ]
]
}
}
]
};
var start = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [4.2400360107421875, 40.143189742924406 ]
}
};
var stop = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [4.3059539794921875, 40.00815750046493 ]
}
};
var sliced = turf.lineSlice(start, stop, line.features[0]);
var length = turf.lineDistance(sliced, 'kilometers');
是否可以使用 turfjs 计算直线路径上两点之间的距离?怎么样?
使用 turf.lineSlice
使用你的两个点对线进行切片。这将 return 一条新线,其中仅包含两点之间线上的部分。接下来你可以使用turf.lineDistance
来计算那条切片线的距离。
var line = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[4.0155029296875, 40.18726672309203 ],
[4.0814208984375, 40.195659093364654 ],
[4.15283203125, 40.19146303804063 ],
[4.185791015625, 40.17887331434696 ],
[4.2242431640625, 40.15788524950653 ],
[4.273681640625, 40.111688665595956 ],
[4.3011474609375, 40.06125658140474 ],
[4.290161132812499, 40.02340800226773 ],
[4.3341064453125, 39.97291055131899 ],
[4.4000244140625, 39.96870074491696 ],
[4.4659423828125, 39.977120098439634 ],
[4.531860351562499, 40.002371935876475 ],
[4.5648193359375, 40.052847601823984 ],
[4.5648193359375, 40.11588965267845 ]
]
}
}
]
};
var start = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [4.2400360107421875, 40.143189742924406 ]
}
};
var stop = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [4.3059539794921875, 40.00815750046493 ]
}
};
var sliced = turf.lineSlice(start, stop, line.features[0]);
var length = turf.lineDistance(sliced, 'kilometers');