将MKMapView缩放到CLRegion

时间:2012-09-13 08:17:31

标签: iphone ios5 mkmapview

我在iOS5上使用CLGeocoder获取CLPlacemark。现在我想取一个地标的区域(CLRegion对象)并将我的MKMapView缩放到该区域,这究竟是怎么回事?

我想要this的倒数,但似乎没有-locationFromLocationWithDistance:或等效的方法。我希望没有人说你必须反过来使用Haversine公式,因为这看起来有点复杂......

2 个答案:

答案 0 :(得分:6)

或者,而不是所有的数学,只需使用API​​为此提供的方法。

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([placemark region].center, [placemark region].radius, [placemark region].radius);
[[self mapView] setCenterCoordinate:[placemark region].center animated:NO];

答案 1 :(得分:1)

您可以使用提供的数学here来完成。并为代码

        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];