使用Google Maps API计算遍历路径的距离

问题描述 投票:-1回答:1

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

我在做什么

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

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

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

问题是,当我收到距离矩阵API的响应时,似乎它给了我源和每条折线编码的坐标的响应,我不确定它是否会考虑整个路径。

我试着阅读文档,但没有帮助。有没有人早先遇到过类似的问题?

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

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

//来自距离Matrix 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;
                })
google-maps google-maps-api-3 google-distancematrix-api
1个回答
0
投票

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

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

有关详细信息,请查看文档:

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

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.