获取Google Maps Api V3中路线服务生成的路线上的所有坐标

时间:2013-03-30 10:44:11

标签: google-maps google-maps-api-3 map-directions

我的应用程序允许用户在地图上选择两个点,并使用Google地图Apiv3的路线服务查找它们之间的路线。然后必须将沿此路线的坐标保存到数据库中。我可以成功编写所有代码来完成此任务。但是我被遗忘了一个问题。

我知道StackOverflow中还有其他几个问题 - OneTwo也是如此,但我认为他们或我在这里错过了一些东西。

示例代码:

function getCoordinates(result) {
            var currentRouteArray = result.routes[0];  //Returns a complex object containing the results of the current route
            var currentRoute = currentRouteArray.overview_path; //Returns a simplified version of all the coordinates on the path


            obj_newPolyline = new google.maps.Polyline({ map: map }); //a polyline just to verify my code is fetching the coordinates
            var path = obj_newPolyline.getPath();
            for (var x = 0; x < currentRoute.length; x++) {
                var pos = new google.maps.LatLng(currentRoute[x].kb, currentRoute[x].lb)
                latArray[x] = currentRoute[x].kb; //Returns the latitude
                lngArray[x] = currentRoute[x].lb; //Returns the longitude
                path.push(pos);
            }
      }

上述代码工作正常,但概述路径的kblb属性似乎保持lat和lng坐标并不总是相同。我上次编写代码的时间为kblb几天,后来更改为mbnb和今天jb和{ {1}}。

我没有看到对象中的任何其他属性可以为我提供除上述之外的其他属性。其他类似问题的答案提到了这个问题。我在这里错过了什么吗?请提供任何可靠的解决方案。

2 个答案:

答案 0 :(得分:12)

不要使用缩小名称和未记录的kb和lb。仅使用记录的属性.lat()和.lng()

答案 1 :(得分:0)

另一种选择是解码“点”字符串:

http://home.provide.net/~bratliff/routes/

比对每个单独的点使用lat()/ lng()方法快得多。