繁体   English   中英

无法将CLLocationCoordinate2D转换为(CLLocationCoordinate2D)

[英]Cannot convert CLLocationCoordinate2D to (CLLocationCoordinate2D)

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

无法将类型'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]()

CLLocationCoordinate2D不符合Equatable

您必须在where闭包中比较latitudelongitude

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

采用

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子句以指定比较的方式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM