gpt4 book ai didi

swift - MKMapItem 创建注释中心

转载 作者:行者123 更新时间:2023-11-30 10:38:50 27 4
gpt4 key购买 nike

我正在尝试执行MKLocalSearch.Request()并在我的mapView上显示结果,类似于this tutorial但是,当我进行搜索时,返回的坐标偏离了应有的中心位置。如果 map 正确地将布鲁克林显示为行政区中心的叠加层,则注释会远离左侧中心。我注意到 MKMapItem 中包含两组坐标,一组是位置,另一组在打印到控制台时显示为中心。我不确定如何访问中心坐标,但中心坐标是屏幕中间的正确坐标。如何访问中心坐标以用作 MKAnnotation 的坐标?谢谢

搜索代码

let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = "brooklyn"
searchRequest.region = MKCoordinateRegion(center: CLLocationCoordinate2DMake(40.758896, -73.985130), latitudinalMeters: 2000, longitudinalMeters: 2000)
let search = MKLocalSearch(request: searchRequest)

search.start { (response, error) in
if let error = error{
print("Error performing search for location")
}

if let response = response{
for item in response.mapItems{
print("map item returned: \(item)")

print("latitude: \(item.placemark.coordinate.latitude) longitude: \(item.placemark.coordinate.longitude)")
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.mapView.addAnnotation(annotation)
}
}
}

输出

map item returned: Optional(CLCircularRegion (identifier:'<+40.64529796,-73.94483550> radius 14090.15', center:<+40.64529796,-73.94483550>, radius:14090.15m)) latitude: 40.6924599 longitude: -73.9903805

最佳答案

我能够通过从 CLRegion 转换 MKMapItemplacemark 属性的 region 属性来访问中心> 到CLCircularRegion

            if let response = response{
for item in response.mapItems{
print("map item returned: \(item.placemark)")
if let region = item.placemark.region as? CLCircularRegion{
print("region.center: \(region.center)")
let annotation = MKPointAnnotation()
annotation.coordinate = region.center
annotation.title = item.name
self.mapView.addAnnotation(annotation)
}

}

关于swift - MKMapItem 创建注释中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57298476/

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