简体   繁体   English

如何从DirectionsResult对象中提取所有经纬度坐标(Google Maps Javascript路线API)

[英]How to extract all lat-long coordinates from DirectionsResult object (Google Maps Javascript directions API)

In the official documentation it is said that : 官方文档中说:

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 DirectionsResult包含路线查询的结果,您可以自己处理,也可以传递给DirectionsRenderer对象,该对象可以自动处理在地图上显示结果

I don't want the directions to be displayed on the map,using DirectionsRenderer object,but I need to access the lat-long coordinates from DirectionsResult object beforehand. 我不希望使用DirectionsRenderer对象在地图上显示路线,但是我需要事先从DirectionsResult对象访问经纬度坐标。

Is there any way in which I can do this ? 有什么办法可以做到这一点?

According to the documentation, a DirectionsResult contains routes property which is an array of DirectionRoute objects. 根据文档, DirectionsResult包含routes属性,该属性是DirectionRoute对象的数组。 Each route has legs which contain coordinates. 每个路线的legs包含坐标。 You should be able to iterate through the properties to access the lat longs of each leg. 您应该能够遍历属性以访问每条腿的纬度。

/**
 * 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 });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM