[英]Retrieve first 'mapItem' element from MKLocalSearch object in swift
first I want to say I'm new to the swift language.首先我想说我是 swift 语言的新手。
My question almost mirrors this question: Accessing MKLocalSearchResponse item (swift)我的问题几乎反映了这个问题: Accessing MKLocalSearchResponse item (swift)
However, when I apply this to my similar looking code I get an error "Value of type 'MKLocalSearch' has no member 'mapItems'"但是,当我将其应用于类似的代码时,出现错误“'MKLocalSearch' 类型的值没有成员'mapItems'”
Like in the link above I want the first mapItems (mapItems[0]) result.就像上面的链接一样,我想要第一个 mapItems (mapItems[0]) 结果。 Can anybody help me?有谁能够帮助我?
Heres my code:这是我的代码:
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)
The variable search
is MKLocalSearch
, so it doesn't has property mapItems
.变量search
是MKLocalSearch
,所以它没有属性mapItems
。 If you want to print the MKMapItem's name
, you should access the mapItems
in the completion block, where you get access to the response
which is MKLocalSearch.Response
.如果您想打印MKMapItem's name
,您应该访问完成块中的mapItems
,在那里您可以访问MKLocalSearch.Response
response
。 The line you write let station1 = response.mapItems[0].name
is perfectly correct and it contains the name of the first mapItems
found您写的行let station1 = response.mapItems[0].name
是完全正确的,它包含找到的第一个mapItems
的名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.