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

前端 未结 3 1802
星月不相逢
星月不相逢 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:31

    By implementing :

    -(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated

    You should be able to reset region to your specific one.

    Look at that answer to have an example of how to do this.


    Another solution, that brings more precise events but is more complex (I'm currently using this successfully for another need) is to subclass MKMapView and override UIScrollViewDelegate protocol.

    If you do that, make sure to call super implementation like that :

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
      if ([super respondsToSelector:@selector(scrollViewWillBeginDragging:)])
        [super scrollViewWillBeginDragging:scrollView];
      // do what you want
    }
    

    Note: While the protocol is public, MKMapView implementation is unknown and might differ with iOS versions, so it is more safe to check with respondsToSelector: before. You must call super implementations or Map will just not work properly.

提交回复
热议问题