Convert CLRegion to MKMapRect or similar

烂漫一生 提交于 2019-12-11 03:58:35

问题


I'd like to convert a CLRegion (center and radius) to two points (top left, bottom right) set of coordinates.

I've seen this answer, but it is not appropriate: Convert MKCoordinateRegion to MKMapRect

The reason I have CLRegion is because it is the output of Forward Geolocation.

I need the two latitude/longitude points to query a database (therefore cannot use CLRegion containsCoordinate).


回答1:


Here is a method to make the conversion between a CLCircularRegion (similar for CLRegion) to a MKMapRect.

Keep in mind that the area returned by the method is a square fitting the CLCircularRegion which is a circle.

- (MKMapRect) rectForCLRegion:(CLCircularRegion *) cicularRegion {
    MKCoordinateRegion region =  MKCoordinateRegionMakeWithDistance(cicularRegion.center, cicularRegion.radius*2, cicularRegion.radius*2);

    MKMapPoint a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                                                                  region.center.latitude + region.span.latitudeDelta / 2,
                                                                  region.center.longitude - region.span.longitudeDelta / 2));
    MKMapPoint b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                                                                  region.center.latitude - region.span.latitudeDelta / 2,
                                                                  region.center.longitude + region.span.longitudeDelta / 2));
    return MKMapRectMake(MIN(a.x,b.x), MIN(a.y,b.y), ABS(a.x-b.x), ABS(a.y-b.y));
}



回答2:


Swift 3 example

let searchRequest = MKLocalSearchRequest(completion: searchCompleter.results[indexPath.row])
let search = MKLocalSearch(request: searchRequest)
search.start { [weak self] response, error in
    guard let region = response?.mapItems.first?.placemark.region as? CLCircularRegion else {
        return
    }
    let mkregion = MKCoordinateRegionMakeWithDistance(region.center, region.radius*2, region.radius*2)
    self?.mapView.setRegion(mkregion, animated: true)
    self?.searchController.isActive = false
}


来源:https://stackoverflow.com/questions/13628755/convert-clregion-to-mkmaprect-or-similar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!