[CLLocation]和CLLocation之间的区别

时间:2017-02-16 06:10:33

标签: ios swift cllocation

我试图找到我从用户抓取的位置与我硬编码的位置之间的距离,但是我收到错误无法将[CLLocation]转换为CLLocation。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude)
     longitude = locations[0].coordinate.longitude
     latitude = locations[0].coordinate.latitude
    let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)

    let distanceInMeters = coordinate₀.distance(from: locations)
    print(distanceInMeters)
}

3 个答案:

答案 0 :(得分:1)

在我看来,你仍然不了解Swift语言的基础知识。也许你应该首先阅读here并首先阅读Swift文档。

您正在使用CLLocation数组,而函数distance(from: )接受单个CLLocation的参数。

您想要做的就是替换

let distanceInMeters = coordinate.distance(from: locations)

let distanceInMeters = coordinate.distance(from: location)

答案 1 :(得分:0)

您需要在下面的行中传递location而不是locations

let distanceInMeters = coordinate₀.distance(from: locations)

答案 2 :(得分:0)

[CLLocation]是一个CLLocation数组,请尝试以下

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude)
     let loc = locations[0]
     longitude = loc[0].coordinate.longitude
     latitude = loc[0].coordinate.latitude
    let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)

    let distanceInMeters = coordinate₀.distance(from: loc)
    print(distanceInMeters)
}