3

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

Community
  • 1
  • 1
Resh32
  • 6,500
  • 3
  • 32
  • 40
  • Can't you use MKCoordinateRegionMakeWithDistance like in the linked question but pass radius*2.0 for the distance? –  Nov 29 '12 at 16:11
  • @AnnaKarenina, but then I'll have a MKCoordinateRegion which does not help either, unless I miss something? – Resh32 Nov 29 '12 at 16:53
  • 1
    From the region, you can derive the MKMapRect or easily calculate the corners as lat/long directly. You can try the first answer in the linked question or use some of the code in [this other answer](http://stackoverflow.com/a/6780374/467105). –  Nov 29 '12 at 17:17
  • @AnnaKarenina this sounds reasonable. Formulate a proper answer so I can accept it - thanks! – Resh32 Nov 30 '12 at 09:25

2 Answers2

5

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
}
Arsonik
  • 2,276
  • 1
  • 16
  • 24
  • `MKCoordinateRegionMakeWithDistance` is deprecated now and `MKCoordinateRegion` should be used instead. Otherwise - great solution! – Dmytro Titov Jul 07 '23 at 18:51
3

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));
}
Ciprian Rarau
  • 3,040
  • 1
  • 30
  • 27