如何使用CLLocations数组作为路径而不是直线计算2个CLLocation点之间的CLLocation距离

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

我有2个CLLocation坐标,我想知道它们之间的距离使用从A点到B的坐标数组的路径。我知道如何计算从A点到B点的距离但问题是它似乎是以直线测量而不是遵循给定路线。有关如何有效地做到这一点的任何想法?

ios swift distance cllocationmanager cllocation
1个回答
1
投票

如果您拥有所有CLLocations,那将非常简单。只需一行代码即可。例如:

     var locations : [CLLocation] = [CLLocation.init(latitude:   CLLocationDegrees(10.00000), longitude: CLLocationDegrees(100.00000)),
     CLLocation.init(latitude: CLLocationDegrees(10.00001), longitude:    CLLocationDegrees(100.00001)),
     CLLocation.init(latitude: CLLocationDegrees(10.00002), longitude: CLLocationDegrees(100.00002)),
     CLLocation.init(latitude: CLLocationDegrees(10.00003), longitude: CLLocationDegrees(100.00003)),
     ]


   let  totalDistance = locations.dropFirst().reduce((locations.first!, 0.0)) {  ($1 , $0.1 + $0.0.distance(from: $1)) }.1


   print(totalDistance)
© www.soinside.com 2019 - 2024. All rights reserved.