3

我在 iOS5 上使用 CLGeocoder 获得 CLPlacemark。现在我想获取地标的区域(CLRegion 对象)并将我的 MKMapView 缩放到该区域,这到底怎么可能?

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

4

2 回答 2

6

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

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([placemark region].center, [placemark region].radius, [placemark region].radius);
[[self mapView] setCenterCoordinate:[placemark region].center animated:NO];
于 2012-11-19T10:26:38.700 回答
1

您可以使用此处提供的数学来完成。对于代码

        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];
于 2012-09-13T08:50:54.110 回答