我有一个本地搜索,为每个搜索结果创建注解。我正在尝试为每个注解添加一个呼叫附件,一旦按下该附件,Map应用程序将打开并设置如何到达特定位置的方向。
我遇到的问题是,为了使呼出附件正确工作,您必须在地址簿导入中使用位置标记获取地址。我已经做了大量的搜索,不知道如何正确设置它,我可以将注解坐标转换为kABPersonAddressStreetKey
,以便Map应用程序可以正确读取它。下面是我的搜索功能和打开Map应用程序功能的代码。
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)
}
任何帮助将不胜感激,提前感谢。
1条答案
按热度按时间mdfafbf11#
你的假设是错误的。您可以像使用地址一样轻松地使用经纬度坐标生成驾驶方向请求。
你应该这么做:
1.获取用户所选管脚的MKMapItem
1.使用MKMapItem方法mapItemForCurrentLocation()为用户的当前位置创建第二个MKMapItem。
1.调用MKMapItem类方法openMapsWithItems(),传入当前位置和目的地的两个MKMapItem,以及指定MKLaunchOptionsDirectionsModeDriving的launchOptions字典。
编辑:
以下是如何为用户点击的项目创建Map项目:
在calloutAccessoryControlTapped方法中,将传递给您一个注解视图。MKAnnotationView类有一个annotation属性,一个符合MKAnnotation属性的对象。
MKAnnotation对象具有坐标属性。
可以从注解视图中获取注解对象,并从注解对象中获取坐标。
然后,您将获取坐标并使用它创建一个MKPlacemark,使用
initWithCoordinate:addressDictionary:
方法(传入一个nil addressDictionary)。这个调用可能看起来像这样:
然后使用
initWithPlacemark:
方法获取这个位置标记并使用它创建一个MKMapItem。获取用户当前位置的MKMapItem:
有一个MKMapView方法
forCurrentLocation()
,它返回当前位置的MKMapItem。