iOS - How to limit the MapView to a specific region?

前端 未结 3 1796
星月不相逢
星月不相逢 2020-11-29 21:38

I have the following problem:

I have a \"drawn map\" (image) which I add to the MapView as an Overlay. No Problem with that.. but I need to limit the MapView to the

3条回答
  •  一生所求
    2020-11-29 22:39

    After trying different ways of limited MKMapView I've concluded that using mapDidChange, and resetting if you're center point goes outside of the boundaries works best, with animated: YES

    Here's how I do it (Using the New Zealand lat/Long span/center).

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ 
      if ((mapView.region.span.latitudeDelta > 15.589921 ) || (mapView.region.span.longitudeDelta > 175.836914) ) {
        CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);
    
        MKCoordinateSpan spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );
    
        MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);
    
        [mapView setRegion: NZRegion animated: YES];
      }
    
     if (abs(abs(mapView.region.center.latitude) - 41.162114) > (13.589921 / 2) ) {
        CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);
    
        MKCoordinateSpan spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );
    
        MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);
    
        [mapView setRegion: NZRegion animated: YES];
    
      }
    
      if (abs(abs(mapView.region.center.longitude) - 172.836914) > (14.062500 / 2) ) {
        CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);
    
         MKCoordinateSpan  spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );
    
         MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);
    
         [mapView setRegion: NZRegion animated: YES];
      }
    }
    

提交回复
热议问题