Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用CLLocation数组而不是直线作为路径计算2个CLLocation点之间的CLLocation距离_Ios_Swift_Distance_Cllocationmanager_Cllocation - Fatal编程技术网

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

Ios 如何使用CLLocation数组而不是直线作为路径计算2个CLLocation点之间的CLLocation距离,ios,swift,distance,cllocationmanager,cllocation,Ios,Swift,Distance,Cllocationmanager,Cllocation,我有两个位置坐标,我想知道它们之间的距离,使用一个从a点到B点的坐标数组作为路径。我知道如何计算从a点到B点的距离,但问题是它似乎是在直线上测量的,而不是沿着给定的路线。你知道如何有效地做到这一点吗?如果你有所有的位置,那就很简单了。只有一行代码可以做到这一点。例如: var locations : [CLLocation] = [CLLocation.init(latitude: CLLocationDegrees(10.00000), longitude: CLLocation

我有两个位置坐标,我想知道它们之间的距离,使用一个从a点到B点的坐标数组作为路径。我知道如何计算从a点到B点的距离,但问题是它似乎是在直线上测量的,而不是沿着给定的路线。你知道如何有效地做到这一点吗?

如果你有所有的位置,那就很简单了。只有一行代码可以做到这一点。例如:

     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)

这不是数组中每对位置之间距离的总和吗?提示:
MKDirectionsRequest
这适用于用户在跟踪时,因此数组是由用户自定义的,我认为MKDirectionsRequest不起作用。此外,用户可能没有手机接收…@eltomatowe虽然这可以在一行中完成,但可读性不强。并不是每个看到这个答案的人都像你一样聪明和/或有经验,在试图弄清楚什么是什么以及它是如何工作的时候可能会迷失方向。我认为你可以通过提供有意义的全名和解释每一步发生的事情来大大改进你的答案。