如何使用MapKit和Swift查找固定项目的位置
我使用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;
}
我现在想能够在注释显示的地址。任何想法将不胜感激。
我还是不明白你想要什么。有时你会询问地址,有时你会询问地点。
的位置的经度和纬度
这是注释的coordinate
财产。
地址是人的邮政地址
当你打电话
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler(// ...
...你回来MKMapItems:
for item in response.mapItems as! [MKMapItem] {
里面每个MKMapItem是一个地标(一MKPlacemark,MKMapItem的placemark
属性)。每个地标的内部都是地址信息 - MKPlacemark是CLPlacemark,因此请参阅CLPlacemark文档以了解如何提取地址信息。
嗯,你附加每个MKMapItem到数组:
matchingItems.append(item as MKMapItem)
这也正是MKMapItems是,除非你放弃你;所以这就是地址信息的地方。
是的,但我如何获得每个特定引脚的位置?我是否需要遍历该数组或...? – kebabTiger
你是一个程序员,你在想什么。你是制作针脚的人。如果你知道你将要在晚些时候将地址与PIN关联起来,请提前计划!以某种方式联系他们! – matt
你在MKMapViewDelegate中实现了mapVIew(_,viewForAnnotation :)吗? – nRewik
因此,这些引脚已添加到MapView中,而您找不到它? – nRewik
我可以找到它,但我想能够知道固定位置的地址。例如,如果我输入咖啡,我会得到大约10场比赛,然后固定在地图上。当我点击图钉时,它会显示该地点的名称,但我想要获取该图钉的位置以便在详细的视图控制器上显示。 – kebabTiger