2011-10-05 98 views
9

我写了下面的代码,如何检测MKMapView缩小/放大?

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated { 
    mapRegion = mapView.region; //MKCoordinateRegion myRegion in .h 
} 

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { 

     MKCoordinateRegion newRegion = mapView.region; 

    if (mapRegion.span.latitudeDelta != newRegion.span.latitudeDelta || 
     mapRegion.span.longitudeDelta != newRegion.span.longitudeDelta){ 

     NSLog(@"The zoom has changed");   
    } 
} 

我想,当用户无论是放大还是缩小了的MKMapView,我应该怎么办做一些操作,但是通过应用上面的代码,如果条件将执行要么使用不会放大/缩小,只是会改变地图?

回答

22

创建MKMapView+ZoomLevel.h作为

#import <MapKit/MapKit.h> 

@interface MKMapView (ZoomLevel) 

- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate 
        zoomLevel:(double)zoomLevel 
        animated:(BOOL)animated; 

@end 

创建您的视图控制器MKMapView+ZoomLevel.m作为

#import "MKMapView+ZoomLevel.h" 

#define MERCATOR_OFFSET 268435456 
#define MERCATOR_RADIUS 85445659.44705395 

@implementation MKMapView (ZoomLevel) 

#pragma mark - 
#pragma mark Map conversion methods 

- (double)longitudeToPixelSpaceX:(double)longitude 
{ 
    return round(MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * M_PI/180.0); 
} 

- (double)latitudeToPixelSpaceY:(double)latitude 
{ 
    return round(MERCATOR_OFFSET - MERCATOR_RADIUS * logf((1 + sinf(latitude * M_PI/180.0))/(1 - sinf(latitude * M_PI/180.0)))/2.0); 
} 

- (double)pixelSpaceXToLongitude:(double)pixelX 
{ 
    return ((round(pixelX) - MERCATOR_OFFSET)/MERCATOR_RADIUS) * 180.0/M_PI; 
} 

- (double)pixelSpaceYToLatitude:(double)pixelY 
{ 
    return (M_PI/2.0 - 2.0 * atan(exp((round(pixelY) - MERCATOR_OFFSET)/MERCATOR_RADIUS))) * 180.0/M_PI; 
} 

#pragma mark - 
#pragma mark Helper methods 

- (MKCoordinateSpan)coordinateSpanWithMapView:(MKMapView *)mapView 
          centerCoordinate:(CLLocationCoordinate2D)centerCoordinate 
           andZoomLevel:(double)zoomLevel 
{ 
    NSLog(@"in custom zoomlevel-->%f",zoomLevel); 
    // convert center coordiate to pixel space 
    double centerPixelX = [self longitudeToPixelSpaceX:centerCoordinate.longitude]; 
    double centerPixelY = [self latitudeToPixelSpaceY:centerCoordinate.latitude]; 

    // determine the scale value from the zoom level 
    double zoomExponent = 20.0 - zoomLevel; 
    double zoomScale = pow(2, zoomExponent); 

    // scale the map’s size in pixel space 
    CGSize mapSizeInPixels = mapView.bounds.size; 
    double scaledMapWidth = mapSizeInPixels.width * zoomScale; 
    double scaledMapHeight = mapSizeInPixels.height * zoomScale; 

    // figure out the position of the top-left pixel 
    double topLeftPixelX = centerPixelX - (scaledMapWidth/2); 
    double topLeftPixelY = centerPixelY - (scaledMapHeight/2); 

    // find delta between left and right longitudes 
    CLLocationDegrees minLng = [self pixelSpaceXToLongitude:topLeftPixelX]; 
    CLLocationDegrees maxLng = [self pixelSpaceXToLongitude:topLeftPixelX + scaledMapWidth]; 
    CLLocationDegrees longitudeDelta = maxLng - minLng; 

    // find delta between top and bottom latitudes 
    CLLocationDegrees minLat = [self pixelSpaceYToLatitude:topLeftPixelY]; 
    CLLocationDegrees maxLat = [self pixelSpaceYToLatitude:topLeftPixelY + scaledMapHeight]; 
    CLLocationDegrees latitudeDelta = -1 * (maxLat - minLat); 

    // create and return the lat/lng span 
    MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta); 
    return span; 
} 

#pragma mark - 
#pragma mark Public methods 

- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate 
        zoomLevel:(double)zoomLevel 
        animated:(BOOL)animated 
{ 
    // clamp large numbers to 28 
    zoomLevel = MIN(zoomLevel, 28); 

    // use the zoom level to compute the region 
    MKCoordinateSpan span = [self coordinateSpanWithMapView:self centerCoordinate:centerCoordinate andZoomLevel:zoomLevel]; 
    MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span); 

    // set the region like normal 
    [self setRegion:region animated:animated]; 
} 

@end 

#import "MKMapView+ZoomLevel.h"声明的MKMapView的拍摄地点。

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    double zoomLevel = [self getZoomLevel]; 
    [lblCurrentLevel setText:[NSString stringWithFormat:@"%.2f",zoomLevel]]; 

    NSLog(@"zoomlevel-->%f long-->%f lat-->%f",zoomLevel,mapView.region.span.longitudeDelta,mapView.region.span.latitudeDelta); 
} 

- (double) getZoomLevel 
{ 
    return 21.00 - log2(objMapView.region.span.longitudeDelta * MERCATOR_RADIUS * M_PI/(180.0 * objMapView.bounds.size.width)); 
} 

在上述方法根据需要,你可以得到缩放级别,你必须做的唯一的事情就是保持初始变焦和输入/输出与之媲美最新的变焦和再次分配给该变量。

在任何查询的情况下,让我知道,我们将制定出的是:d

编辑

此源代码是从Troy's博客。对于之前不提的道歉。

+0

@谢谢你珍妮斯,我会申请它然后会通知你,我可以直接与你联系吗? –

+0

当您阅读此评论时,请告知我,我正在等待您。 –

+0

coordinateSpanWithMapView实际上存在一个错误的错误。要计算zoomExponent,你必须从21减去,而不是20; getZoomLevel是正确的。 –