gpt4 book ai didi

ios - Swift - 从坐标获取地址

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:35 28 4
gpt4 key购买 nike

我有一个本地搜索,可以为每个搜索结果创建注释。我正在尝试为每个注释添加一个标注附件,一旦按下该附件, 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. 获取用户选择的图钉的 MKMapItem

  2. 使用MKMapItem 方法 mapItemForCurrentLocation()。

  3. 调用 MKMapItem 类方法 openMapsWithItems() 传入当前位置和目的地的两个 MKMapItems,以及一个指定 MKLaunchOptionsDirectionsModeDriving 的 launchOptions 字典。

编辑:

以下是为用户点击的项目创建 map 项目的方法:

在您的 calloutAccessoryControlTapped 方法中,您获得了一个注释 View 。MKAnnotationView类有一个注解属性,一个符合MKAnnotation属性的对象。

一个 MKAnnotation 对象有一个坐标属性。

您可以从注释 View 中获取注释对象,并从注释对象中获取坐标。

然后,您将使用 initWithCoordinate:addressDictionary: 方法(传入 nil addressDictionary)获取坐标并使用它创建 MKPlacemark。

该调用可能如下所示:

let sourcePlacemark = MKPlacemark(
coordinate: fromCoordinate,
addressDictionary: nil)

然后,您可以使用 initWithPlacemark: 方法获取位置标记并使用它来创建 MKMapItem。

获取用户当前位置的 MKMapItem:

只有一个 MKMapView 方法 mapItemForCurrentLocation 返回当前位置的 MKMapItem。

关于ios - Swift - 从坐标获取地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30040853/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com