将MKMapView缩放到CLRegion [英] Zoom MKMapView to CLRegion

查看:73
本文介绍了将MKMapView缩放到CLRegion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS5上使用CLGeocoder获得了CLPlacemark.现在,我想获取一个地标的区域(CLRegion对象),然后将我的MKMapView缩放到该区域,这到底有可能吗?

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?

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

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

推荐答案

您可以使用此处.对于代码

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

这篇关于将MKMapView缩放到CLRegion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆