我有一个本地搜索功能,为每个搜索结果创建注释。我尝试为每个注释添加一个呼叫附件,一旦按下该附件,地图应用程序将打开并设置方向以到达特定位置。
我遇到的问题是,为了使呼叫附件正常工作,必须使用地址簿导入中的地点标记获取地址。我已经进行了大量搜索,但无法弄清楚如何正确设置它,以便我可以将注释坐标转换为
任何帮助都将不胜感激,提前致谢。
我遇到的问题是,为了使呼叫附件正常工作,必须使用地址簿导入中的地点标记获取地址。我已经进行了大量搜索,但无法弄清楚如何正确设置它,以便我可以将注释坐标转换为
kABPersonAddressStreetKey
,以便地图应用程序可以正确读取它。以下是我的搜索功能和打开地图应用程序功能的代码。func performSearch() -> MKMapItem {
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.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)")
self.matchingItems.append(item as MKMapItem)
println("Matching items = \(self.matchingItems.count)")
var annotation = MKPointAnnotation()
var coordinates = annotation.coordinate
var location = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude)
var street = // Insert code here for getting address from coordinates
var addressDictionary = [String(kABPersonAddressStreetKey): street]
var placemark = MKPlacemark(coordinate: coordinates, addressDictionary: addressDictionary)
var mapItem = MKMapItem(placemark: placemark)
return mapItem
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.mapView.addAnnotation(annotation)
}
}
})
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
calloutAccessoryControlTapped control: UIControl!) {
let location = view.annotation as! FirstViewController
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
location.performSearch().openInMapsWithLaunchOptions(launchOptions)
}
任何帮助都将不胜感激,提前致谢。