首先,我想说我是swift语言的新手。
我的问题几乎反映了这个问题: Accessing MKLocalSearchResponse item (swift)
然而,当我将它应用于类似的代码时,我得到了一个错误“类型为'MKLocalSearch‘的值没有成员'mapItems'”
就像上面的链接一样,我想要第一个mapItems (mapItems)结果。有人能帮我吗?
下面是我的代码:
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)
发布于 2019-02-22 04:36:31
变量search
为MKLocalSearch
,因此它没有属性mapItems
。如果您想打印MKMapItem's name
,您应该访问完成块中的mapItems
,在这里您可以访问response
,也就是MKLocalSearch.Response
。您编写的let station1 = response.mapItems[0].name
行是完全正确的,并且它包含找到的第一个mapItems
的名称
https://stackoverflow.com/questions/54814922
复制相似问题