问题
I am getting a CLPlacemark using the CLGeocoder on iOS5. Now I would like to take the region (CLRegion object) of a placemark and have my MKMapView zoom to that region, how on earth is this possible?
I want the inverse of this, but there seems to be no -locationFromLocationWithDistance:
or equivalent method. I'm hoping nobody says you have to use the Haversine formula in reverse because that looks a tad complicated...
回答1:
Or, instead of all that math, just use the methods provided for this by the API.
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([placemark region].center, [placemark region].radius, [placemark region].radius);
[[self mapView] setCenterCoordinate:[placemark region].center animated:NO];
回答2:
You can do it using the mathematics provided here. And for the code
CLLocationCoordinate2D center = placemark.location.coordinate;
CLRegion* coreLocationRegion = placemark.region;
CLLocationDistance radius = coreLocationRegion.radius;
#define kBEARING_NORTH 0.0
#define kBEARING_EAST .5 * M_PI
#define kBEARING_SOUTH M_PI
#define kBEARING_WEST 1.5 * M_PI
#define kEARTH_RADIUS_M 6371000.0
// Store the angular distance of each side from the center
double angDist = radius / kEARTH_RADIUS_M;
// Convert center lat and lng to radians
double centerLatRad = center.latitude * M_PI / 180;
double centerLngRad = center.longitude * M_PI / 180;
// Calculate latitude range
double maxLatRad = asin(sin(centerLatRad) * cos(angDist) +
cos(centerLatRad) * sin(angDist) * cos(kBEARING_NORTH));
double minLatRad = asin(sin(centerLatRad) * cos(angDist) +
cos(centerLatRad) * sin(angDist) * cos(kBEARING_SOUTH));
// Calculate longitude range
// Longitude range requires coresponding latitudes:
double tempLatRad;
// Calculate max longitude
tempLatRad = asin(sin(centerLatRad) * cos(angDist) +
cos(centerLatRad) * sin(angDist) * cos(kBEARING_EAST));
double maxLngRad = centerLngRad + atan2(sin(kBEARING_EAST) * sin(angDist) * cos(centerLatRad),
cos(angDist) - sin(centerLatRad) * sin(tempLatRad));
// Calculate min longitude
tempLatRad = asin(sin(centerLatRad) * cos(angDist) +
cos(centerLatRad) * sin(angDist) * cos(kBEARING_WEST));
double minLngRad = centerLngRad + atan2(sin(kBEARING_WEST) * sin(angDist) * cos(centerLatRad),
cos(angDist) - sin(centerLatRad) * sin(tempLatRad));
CLLocationDegrees latitudeDelta = (maxLatRad-minLatRad) * 180 / M_PI;
CLLocationDegrees longitudeDelta = (maxLngRad-minLngRad) * 180 / M_PI;
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[self.mapView setRegion: region animated: YES];
来源:https://stackoverflow.com/questions/12402113/zoom-mkmapview-to-clregion