使用Google Maps API计算行进路径的距离

时间:2018-09-06 05:12:02

标签: google-maps google-maps-api-3 google-distancematrix-api

要求是跟踪用户的坐标并在一天结束时找出驾驶员公里数。

我在做什么

第1步:记录设备收到的所有坐标

第2步:将所有坐标编码为折线编码的字符串

第3步:使用距离矩阵API,计算源和编码的折线之间的距离

问题是,当我收到来自Distance Matrix API的响应时,似乎给了我源和以折线编码的每个坐标的响应,我不确定它是否会考虑整个路径。

我试图阅读文档,但没有帮助。有人有类似的问题吗?

//获取编码路径并找到距离

let source      = coordinates.remove(at: 0)
let polyline    = Polyline.init(coordinates: coordinates)
let encodedPath = polyline.encodedPolyline
requestCalculateDistance(forSource: source, encodedPath: encodedPath)

//来自距离矩阵API的响应

rows =     (
                {
            elements =             (
                                {
                    distance =                     {
                        text = "0.1 km";
                        value = 135;
                    };
                    duration =                     {
                        text = "1 min";
                        value = 4;
                    };
                    status = OK;
                },
                                {
                    distance =                     {
                        text = "0.1 km";
                        value = 135;
                    };
                    duration =                     {
                        text = "1 min";
                        value = 4;
                    };
                    status = OK;
                })

1 个答案:

答案 0 :(得分:0)

我相信您可以使用Geometry library的Google Maps JavaScript API来解决您的问题。

如前所述,您收集了形成折线的所有坐标,因此有一个LatLng对象数组。为了计算折线的长度,您可以使用google.maps.geometry.spherical.computeLength()Array<LatLng>作为第一个参数的MVCArray<LatLng>方法。

有关更多详细信息,请参阅文档:

https://developers.google.com/maps/documentation/javascript/reference/geometry#spherical.computeLength

我希望这会有所帮助!

相关问题