正在计算行程距离核心位置 swift

Calculating trip distance core location swift

我有一个应用程序可以像 Uber 应用程序一样计算行驶距离。当 driver 开始旅行时,即使在搜索乘车时指定了起点,位置也会开始改变,driver 可能会决定通过替代路线或通过长途地点和路线因为他/她不知道最短路线,那我怎么计算总距离。

起始位置是driver点击开始按钮的位置 结束位置是driver点击停止按钮

的位置

到目前为止,这是我的代码

    public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        lastLocation = locations.last!
        endTrip(locations.last)

        if !hasSetInitialLocation {

            let camera = GMSCameraPosition.camera(withTarget: lastLocation!.coordinate, zoom: 17)
            self.mapView.animate(to: camera)
            hasSetInitialLocation = true
            endTrip(lastLocation)
            MqttManager.instance.connectToServer()
        }
    }



func endTrip(endLoaction: CLLocation) {
        guard let statusChange = source.getStatusChange() else{return}
        var distanceTraveled: Double = 0.0
        let initialLocation = CLLocation(latitude: (statusChange.meta?.location?.lat)!, longitude: (statusChange.meta?.location?.lng)!)
        let distance = initialLocation.distance(from: endLoaction)
        distanceTraveled += distance
        let distanceInKM = Utility.convertCLLocationDistanceToKiloMeters(targetDistance: distanceTraveled)
}

我如何计算距离以反映 driver 移动的总距离,因为建议的起点和终点的路线可能会发生变化。

driver点击了一个叫做开始行程的按钮,我想得到从那一刻到他点击按钮结束行程的那一刻的距离

这个实现可以从类似的工作代码中获得,但唯一的区别是它们是一个开始按钮,它传递该点的坐标和一个停止坐标,它是坐标的终点。

enum DistanceValue: Int {
                case meters, miles
            }

            func calculateDistanceBetweenLocations(_ firstLocation: CLLocation, secondLocation: CLLocation, valueType: DistanceValue) -> Double {
                var distance = 0.0
                let meters = firstLocation.distance(from: secondLocation)
                distance += meters
                switch valueType {
                case .meters:
                    return distance
                case .miles:
                    let miles = distance
                    return miles
                }
            }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            if startLocation == nil {
                startLocation = locations.first
            } else if let location = locations.last {
                runDistance += lastLocation.distance(from: location)
                let calc = calculateDistanceBetweenLocations(lastLocation, secondLocation: location, valueType: .meters)

                print("TOTAL LOC 1 \(calc)")
                print("TOTAL LOC 2 \(runDistance)")
            }
            lastLocation = locations.last

        }

如我的打印语句所示 print("TOTAL LOC 1 \(calc)") print("TOTAL LOC 2 \(runDistance)") 我该如何制作

calcrunDistance

这是控制台中打印的内容

TOTAL LOC 10.29331530774379
TOTAL LOC 2 10.29331530774379
TOTAL LOC 2.2655118031831587
TOTAL LOC 2 12.558827110926948

如果你使用第一个和最后一个坐标得到这样的距离,它总是returns错误的值,因为它无法识别实际的行进路径。

我确实使用以下代码解决了同样的问题。

使用谷歌地图

> pod 'GoogleMaps'

在driver在路线上移动时制作坐标数组。

var arr = [Any]() 
// Driving lat long co-ordinateds continues add in this array according to your expectation either update location or perticuler time duration.

// make GMSMutablePath of your co-ordinates
let path = GMSMutablePath()

    for obj in arr{

        print(obj)

        if let lat = (obj as? NSDictionary)?.value(forKey: PARAMETERS.LET) as? String{

            path.addLatitude(Double(lat)!, longitude: Double(((obj as? NSDictionary)?.value(forKey: PARAMETERS.LONG) as? String)!)!)

        }
    }

print(path) // Here is your traveling path
let km = GMSGeometryLength(path)
print(km) // your total traveling distance.

我在 this 应用程序中完成了它并且工作正常。 希望对你有帮助:)

或没有 GoogleMaps

不过,根据您的代码,您必须为自己提供位置,一个 CLLocationCoordinate2D 数组。

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    // MARK: - Variables
    let locationManager = CLLocationManager()

    // MARK: - IBOutlet
    @IBOutlet weak var mapView: MKMapView!

    // MARK: - IBAction
    @IBAction func distanceTapped(_ sender: UIBarButtonItem) {
        let locations: [CLLocationCoordinate2D] = [...]
        var total: Double = 0.0
        for i in 0..<locations.count - 1 {
            let start = locations[i]
            let end = locations[i + 1]
            let distance = getDistance(from: start, to: end)
            total += distance
        }
        print(total)
    }

    func getDistance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
        // By Aviel Gross
        // https://whosebug.com/questions/11077425/finding-distance-between-cllocationcoordinate2d-points
        let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
        let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
        return from.distance(from: to)
    }
}

输出

给定 CLLocationCoordinate2D 数组计算距离(以米为单位)的简单函数。使用 reduce 而不是数组迭代。

func computeDistance(from points: [CLLocationCoordinate2D]) -> Double {
    guard let first = points.first else { return 0.0 }
    var prevPoint = first
    return points.reduce(0.0) { (count, point) -> Double in
        let newCount = count + CLLocation(latitude: prevPoint.latitude, longitude: prevPoint.longitude).distance(
            from: CLLocation(latitude: point.latitude, longitude: point.longitude))
        prevPoint = point
        return newCount
    }
}

我喜欢为此使用扩展程序

extension Array where Element: CLLocation {
    
    var distance: Double {
        guard count > 1 else { return 0 }
        var previous = self[0]
        
        return reduce(0) { (result, location) -> Double in
            let distance = location.distance(from: previous)
            previous = location
            return result + distance
        }
    }
    
}

用法:

locations.distance