问题
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