我使用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