限制MKMapView显示的区域

限制MKMapView显示的区域: 自定义一个显示范围 (根据 centerCoord 和 span 来确定地图的初始 Region),拖动或者缩放都不能超过限定的范围。

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    
    //限制map显示区域
    /*
     newLocCoordinate : 初始的中心点经纬度
     oriSpan :初始的Span
     
    */
    MKCoordinateRegion regin = mapView.region;
  
    if ((regin.span.latitudeDelta > oriSpan.latitudeDelta ) || (regin.span.longitudeDelta > oriSpan.longitudeDelta) ) {
        
        
        CLLocationCoordinate2D centerCoord = newLocCoordinate;
        
        MKCoordinateSpan span = oriSpan;
        
        MKCoordinateRegion region = MKCoordinateRegionMake(centerCoord, span);
        
        [mapView setRegion: region animated: YES];
    }
    
    if (regin.center.latitude+regin.span.latitudeDelta/2 > newLocCoordinate.latitude+oriSpan.latitudeDelta/2
        ||regin.center.latitude-regin.span.latitudeDelta/2 < newLocCoordinate.latitude-oriSpan.latitudeDelta/2) {
        CLLocationCoordinate2D centerCoord = newLocCoordinate;
        
        MKCoordinateSpan span = oriSpan;
        
        MKCoordinateRegion region = MKCoordinateRegionMake(centerCoord, span);
        
        [mapView setRegion: region animated: YES];
        
    }
    
    if (regin.center.longitude+regin.span.longitudeDelta/2 > newLocCoordinate.longitude+oriSpan.longitudeDelta/2
        ||regin.center.longitude-regin.span.longitudeDelta/2 < newLocCoordinate.longitude-oriSpan.longitudeDelta/2) {
        CLLocationCoordinate2D centerCoord = newLocCoordinate;
        
        MKCoordinateSpan  span = oriSpan;
        
        MKCoordinateRegion region = MKCoordinateRegionMake(centerCoord, span);
        
        [mapView setRegion: region animated: YES];
    }
}


你可能感兴趣的:(范围,地图,MkMapView,zoom)