Google Maps JavaScript API v3方向功能 [英] Google Maps JavaScript API v3 directions capabilities

查看:111
本文介绍了Google Maps JavaScript API v3方向功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Maps js API v3。我可以使用基于的航点显示路线。我想要的是获得方向的要点。可以说方向线从布达佩斯到华沙。我想要获得该线上的所有经度和纬度。但是我没有找到任何构建函数或解决方法。我不复制代码此处为教程适用于我,我可以从此开始。
(当然这不是一个好主意)。



我见过这个问题。这不是我的问题的解决方案,因为它不适用于API3



感谢您提前。



更新:基于Matty F的回答,我发现我搜索了结果对象的路由数组的overview_path字段。谢谢。

解决方案

当调用 DirectionsService.route 作为第二个参数的回调。回调中的第一个参数是 DirectionsResult 对象。通过循环这个对象的子元素来获取指示的所有点:

  DirectionsService.route(request,function(result,状态){
var pointsArray = [];

(result.routes中的var路径){
(route.legs中的var leg){
for (var step in leg.steps){
(step.path中的变量latlng){
pointsArray.push(latlng)
}
}
{
}
});

最后,pointsArray将保存起点和终点之间的所有LatLng点。


I use Google Maps js API v3. I can show directions with waypoints based on this. What I want is getting the points of the direction. Lets say the direction line goes from Budapest to Warsaw. I want to get all latitude and longitude on that line. But I don't find any build in function or workaround for this. I don't copy code here as the tutorial works for me, I can start from that. (Of course it isn't a good idea to make lot of waypoints.)

I've seen this question. It isn't solution for my problem, as its not for API3

Thanks in advance.

UPDATE: Based on Matty F's answer I found out I searched for the result object's routs array's overview_path field. Thank you.

解决方案

When calling the DirectionsService.route method, pass a callback as the second parameter. The first parameter in the callback will be a DirectionsResult object. Get all the points of the directions by looping through the children of this object:

DirectionsService.route(request, function(result, status) {
    var pointsArray = [];

    for (var route in result.routes) {
        for (var leg in route.legs) {
            for (var step in leg.steps) {
                for (var latlng in step.path) {
                    pointsArray.push(latlng)
                }
            }
        {
    }
});

In the end, pointsArray will hold all the LatLng points between the start and destination points.

这篇关于Google Maps JavaScript API v3方向功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆