在Google Maps API v3的Directions Service生成的路线中获取所有坐标

13

我的应用程序允许用户在地图上选择两个点,并使用Google Maps APIv3的Directions Service找到它们之间的路线。然后,沿着这条路线的坐标必须保存到数据库中。我已经成功编写了所有代码来完成这个任务。但是,我遇到了一个问题。

我知道StackOverflow上有几个类似的问题- 一个两个,但我认为肯定他们或者我在这里漏掉了某些东西。

示例代码:

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属性不总是相同之外,上述代码完美运行。上次我编写代码时,它持续了几天,后来变成了mbnb,今天又是jbkb

我没有看到对象中提供其他经纬度信息的属性。回答其他类似问题的答案没有提到这个问题。我是否漏掉了什么?请提供可靠的解决方案。

2个回答

12
请勿使用未经记录的缩写kb和lb。请仅使用已记录的属性.lat()和.lng()。

0

另一个选择是解码“points”字符串:

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

使用一次性的lat() / lng()方法比为每个单独点使用这些方法要快得多。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接