0

我正在尝试MKLocalSearch.Request()在我的 mapView 上执行和显示结果,类似于本教程,但是当我进行搜索时,返回的坐标偏离了它们应该在的位置。如果地图正确地将布鲁克林显示为自治市镇中心的叠加层,则注记远离左侧中心。我注意到有两组坐标包含在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)
        }
    }
}

输出

返回的地图项:可选(CLCircularRegion(标识符:'<+40.64529796,-73.94483550>半径14090.15',中心:<+40.64529796,-73.94483550>,半径:14090.15m))纬度:40.6924599经度:-73.9903055

4

1 回答 1

0

我可以通过将' 属性的region属性从转换为 来访问中心。MKMapItemplacemarkCLRegionCLCircularRegion

            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)
                    }

                }
于 2019-07-31T20:56:01.647 回答