-1

首先我想说我是 swift 语言的新手。

我的问题几乎反映了这个问题: Accessing MKLocalSearchResponse item (swift)

但是,当我将其应用于外观相似的代码时,出现错误“'MKLocalSearch' 类型的值没有成员'mapItems'”

就像上面的链接一样,我想要第一个 mapItems (mapItems[0]) 结果。有谁能够帮我?

这是我的代码:

    let latitude = String(currentLocation.coordinate.latitude)
    let longitude = String(currentLocation.coordinate.longitude)

    var station1Unwrapped: String! = ""
    var station2Unwrapped: String! = ""

    var coord: CLLocationCoordinate2D!
    coord = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    var region: MKCoordinateRegion!
    region = MKCoordinateRegion(center: coord, latitudinalMeters: 100, longitudinalMeters: 100);

    let request = MKLocalSearch.Request()
    request.naturalLanguageQuery = "Train Station"
    request.region = region

    let search = MKLocalSearch(request: request)
    search.start { response, error in
        guard let response = response else {
            print("There was an error searching for: \(String(describing: request.naturalLanguageQuery)) error: \(String(describing: error))")
            return
        }

        print("Inside function")

        let station1 = response.mapItems[0].name


    }

    var newLocVar = (search.mapItems[0] as! MKMapItem).name
    print(newLocVar)
4

1 回答 1

0

变量searchMKLocalSearch,所以它没有属性mapItems。如果要打印MKMapItem's name,则应mapItems在完成块中访问 ,您可以在其中访问responsewhich is MKLocalSearch.Response。你写的那行let station1 = response.mapItems[0].name是完全正确的,它包含第一个mapItems找到的名字

于 2019-02-21T20:36:31.390 回答