gpt4 book ai didi

javascript - 如何从 DirectionsResult 对象中提取所有经纬度坐标(Google map Javascript 方向 API)

转载 作者:行者123 更新时间:2023-12-03 02:14:41 30 4
gpt4 key购买 nike

official documentation据说:

The DirectionsResult contains the result of the directions query, which you may either handle yourself, or pass to a DirectionsRenderer object, which can automatically handle displaying the result on a map

我不希望使用 DirectionsRenderer 对象在 map 上显示路线,但我需要事先从 DirectionsResult 对象访问经纬度坐标。

有什么办法可以做到这一点吗?

最佳答案

根据文档,DirectionsResult 包含 routes 属性,该属性是 DirectionRoute 对象的数组。每条路线都有包含坐标的legs。您应该能够迭代属性来访问每条腿的经纬度。

/**
* Assuming you have a directions result
* assigned to const myDirectionsResult
*/

// es6 syntax

// const myDirectionsResult = ...

const legCoordinates = [];

myDirectionsResult.routes.forEach(route => {
route.legs.forEach(leg => {
const {start_location, end_location} = leg;

legCoordinates.push({ start_location, end_location });
});
});

关于javascript - 如何从 DirectionsResult 对象中提取所有经纬度坐标(Google map Javascript 方向 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49415597/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com