我使用MapKit的localsearch功能來生成任何輸入到搜索欄中的註釋引腳。我現在無法找到這些固定位置的地址。這是我的本地搜索如何使用MapKit和Swift查找固定項目的位置
func performSearch() {
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text;
request.region = attractionsMap.region;
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler({(response:
MKLocalSearchResponse!,
error: NSError!) in
if error != nil {
println("Error occured in search: \(error.localizedDescription)")
} else if response.mapItems.count == 0 {
println("No matches found")
} else {
println("Matches found")
for item in response.mapItems as! [MKMapItem] {
println("Name = \(item.name)")
println("Phone = \(item.phoneNumber)")
matchingItems.append(item as MKMapItem)
println("Matching items = \(matchingItems.count)")
var annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.attractionsMap.addAnnotation(annotation)
}
}
})
}
代碼這是我的viewForAnnotation方法:
extension AttractionsVC: MKMapViewDelegate {
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if(annotation is MKUserLocation) {
return nil;
}
let reuseId = "pin";
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView;
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId);
pinView!.canShowCallout = true;
pinView!.animatesDrop = true;
}
var moreInfoButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton;
pinView?.rightCalloutAccessoryView = moreInfoButton;
return pinView;
}
我現在想能夠在註釋顯示的地址。任何想法將不勝感激。
你在MKMapViewDelegate中實現了mapVIew(_,viewForAnnotation :)嗎? – nRewik
因此,這些引腳已添加到MapView中,而您找不到它? – nRewik
我可以找到它,但我想能夠知道固定位置的地址。例如,如果我輸入咖啡,我會得到大約10場比賽,然後固定在地圖上。當我點擊圖釘時,它會顯示該地點的名稱,但我想要獲取該圖釘的位置以便在詳細的視圖控制器上顯示。 – kebabTiger