无法将CLLocationCoordinate2D转换为(CLLocationCoordinate2D)

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

我的代码有问题。我收到此错误消息:

无法将'CLLocationCoordinate2D'类型的值转换为预期的参数类型'(CLLocationCoordinate2D)throws - > Bool'

if(locationManager.allowsBackgroundLocationUpdates && isDriving){
        locationManager.startUpdatingLocation()
        guard let locValue: CLLocationCoordinate2D = locationManager.location?.coordinate else { return }
        Map.setCenter(locValue, animated: true)
        Map.setRegion(MKCoordinateRegion(center: locValue, latitudinalMeters: 75, longitudinalMeters: 75), animated: true)

        if !locations.contains(where: locValue) { //<- ERROR
            locations.append(locValue);
            NSLog("%f %f -> Gesamt: %d", locValue.latitude, locValue.longitude, locations.count);
            let polyline = MKPolyline(coordinates: &locations, count: locations.count);
            Map.addOverlay(polyline);
        }
    }

地点:

var locations = [CLLocationCoordinate2D]()
ios swift contains
2个回答
3
投票

CLLocationCoordinate2D不符合Equatable

你需要在latitude闭合中比较longitudewhere

if !locations.contains(where: {$0.latitude == locValue.latitude && $0.longitude == locValue.longitude}) { ...

2
投票

使用

extension CLLocationCoordinate2D : Equatable { 
    static public func ==(left: CLLocationCoordinate2D, right: CLLocationCoordinate2D) -> Bool {
        return left.latitude == right.latitude && left.longitude == right.longitude
    } 
}

if !locations.contains(locValue){  

}

对于使用contains的数组,元素需要符合Equatable,并且因为CLLocationCoordinate2D不符合,所以错误修复需要你添加一个where子句来指定比较将如何

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