簡體   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