Google Maps API - 沿路线的中点 [英] Google Maps API - Midpoint along route

查看:195
本文介绍了Google Maps API - 沿路线的中点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩Google Maps / Google方向API。有没有人对我如何能够沿着路线而不是地理中点找到中点提出了一些看法。



理想情况下,我想找到这个中点的经纬值。



有什么想法?我有点难过,希望我能够找到一个建议,而不会疯狂地尝试自己找到答案。

解决方案

最简单的方法是,您可以先:

1 )使用GMSGeometryDistance计算路径的总距离,通过GMSGeometryDistance函数计算每两个后续点之间的距离,然后总和所有距离。2)然后再次计算,并在每一步中加总。当总和大约是总距离的一半时,那么你处于中间点。
示例代码如下:

pre $ func findTotalDistanceOfPath(path:GMSPath) - > Double {

let numberOfCoords = path.count()

var totalDistance = 0.0

如果numberOfCoords> 1 {

var index = 0 as UInt

while index< numberOfCoords {

//1.1 cal下一个距离

var currentCoord = path.coordinateAtIndex(index)

var nextCoord = path.coordinateAtIndex(index + 1)

var newDistance = GMSGeometryDistance(currentCoord,nextCoord)

totalDistance = totalDistance + newDistance

index = index + 1



$ b return totalDistance


$ b func findMiddlePointInPath(path:GMSPath,totalDistance distance:Double) - > CLLocationCoordinate2D? {

let numberOfCoords = path.count()

let halfDistance = distance / 2

let threadhold = 10 // 10 meters

var midDistance = 0.0

如果numberOfCoords> 1 {

var index = 0 as UInt

while index< numberOfCoords {

//1.1 cal下一个距离

var currentCoord = path.coordinateAtIndex(index)

var nextCoord = path.coordinateAtIndex(index + 1)

var newDistance = GMSGeometryDistance(currentCoord,nextCoord)

midDistance = midDistance + newDistance

if fabs(midDistance - halfDistance)< threadhold {//找到路线中的中点

return nextCoord

}

index = index + 1

}

}
return nil //如果我们因为某些原因无法在路径中找到中间点,则返回nil
}

还有更多优化功能。我在此处在Swift中写了一个详细的答案


I've been playing around with the Google Maps/Google directions API. Does anyone have an inkling on how I might be able to find the mid-point along a route, not the geographic midpoint.

Ideally I'd like to find the lat and long values of this midpoint.

Any thoughts? I'm a bit stumped and hoping I may be able to find a suggestion without going crazy trying to find the answer myself.

解决方案

An easiest way is that you can first:

1) Calculate total distance of the path using GMSGeometryDistance, by calculating the distance between every two subsequent points by GMSGeometryDistance function, then summing all the distance.

2) Then you calculate again, and summing in each step. When the sum is about half of the total distance, then you are at the middle point. Sample code is as following:

    func findTotalDistanceOfPath(path: GMSPath) -> Double {

        let numberOfCoords = path.count()

        var totalDistance = 0.0

        if numberOfCoords > 1 {

            var index = 0 as UInt

            while index  < numberOfCoords{

                //1.1 cal the next distance

                var currentCoord = path.coordinateAtIndex(index)

                var nextCoord = path.coordinateAtIndex(index + 1)

                var newDistance = GMSGeometryDistance(currentCoord, nextCoord)

                totalDistance = totalDistance + newDistance

                index = index + 1

             }

        }
return totalDistance

    }

func findMiddlePointInPath(path: GMSPath ,totalDistance distance:Double) -> CLLocationCoordinate2D? {

    let numberOfCoords = path.count()

    let halfDistance = distance/2

    let threadhold = 10 //10 meters

    var midDistance = 0.0

    if numberOfCoords > 1 {

        var index = 0 as UInt

        while index  < numberOfCoords{

            //1.1 cal the next distance

            var currentCoord = path.coordinateAtIndex(index)

            var nextCoord = path.coordinateAtIndex(index + 1)

            var newDistance = GMSGeometryDistance(currentCoord, nextCoord)

            midDistance = midDistance + newDistance

            if fabs(midDistance - halfDistance) < threadhold { //Found the middle point in route

                return nextCoord

            }

            index = index + 1

        }

    }
    return nil //Return nil if we cannot find middle point in path for some reason
}

There is more to optimize the function. I wrote a detail answer in Swift in here

这篇关于Google Maps API - 沿路线的中点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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