Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS - How to limit the MapView to a specific region?
    text
    copied!<p>I have the following problem:</p> <p>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 region of the Overlay, so a user isn't able to scroll/zoom outside of this region.. but it should be possible to scroll/zoom inside the "bounds" of the overlay - means I cannot just disable zoom/scrolling for the MapView.</p> <p>Are there any ideas/solution on this topic? The reason for using the MapView/-Kit is that I need to add various POIs to the custom map. This may become more complex when just using an ImageView+ScrollView for presenting the custom map.</p> <p>I've researched alot on this topic, but I didn't found a nice solution.</p> <p>Any help is appreciated!</p> <p>Best Regards, Christian</p> <p><strong>Edit: This is our solution:</strong> <em>You supply a topleft and a bottomright coordinate to limit the map. The (minimum) zoomlevel is also limited. I've deactivated decelerating and you are able to bounce a bit out of the map (for better performance/ux). I added a ~1km grey border to the overlay so the user isnÄt able to see the orignal worldmap of google.</em></p> <p><strong>LimitedMapView.h:</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;MapKit/MapKit.h&gt; @interface LimitedMapView : MKMapView &lt;UIScrollViewDelegate&gt;{ } @property (nonatomic, assign) CLLocationCoordinate2D topLeftCoordinate; @property (nonatomic, assign) CLLocationCoordinate2D bottomRightCoordinate; @end </code></pre> <p><strong>LimitedMapView.m:</strong></p> <pre><code>#import "LimitedMapView.h" @implementation LimitedMapView @synthesize topLeftCoordinate, bottomRightCoordinate; - (void)scrollViewDidZoom:(UIScrollView *)scrollView{ if([super respondsToSelector:@selector(scrollViewDidZoom:)]) [super scrollViewDidZoom:scrollView]; if ([self region].span.latitudeDelta &gt; 0.002401f || [self region].span.longitudeDelta &gt; 0.003433f) { CLLocationCoordinate2D center = self.centerCoordinate; MKCoordinateSpan span = MKCoordinateSpanMake(0.002401f, 0.003433f); self.region = MKCoordinateRegionMake(center, span); } } -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ if([super respondsToSelector:@selector(scrollViewDidEndDragging:)]) [super scrollViewDidEndDragging:scrollView willDecelerate:decelerate]; MKCoordinateRegion currentRegion = self.region; bool changeRegionLong = YES; bool changeRegionLat = YES; // LONGITUDE if((currentRegion.center.longitude - (currentRegion.span.longitudeDelta/2)) &lt; self.topLeftCoordinate.longitude) { currentRegion.center.longitude = (topLeftCoordinate.longitude + (currentRegion.span.longitudeDelta/2)); } else if((currentRegion.center.longitude + (currentRegion.span.longitudeDelta/2)) &gt; self.bottomRightCoordinate.longitude) { currentRegion.center.longitude = (bottomRightCoordinate.longitude - (currentRegion.span.longitudeDelta/2)); } else { changeRegionLong = NO; } // LATITUDE if((currentRegion.center.latitude + (currentRegion.span.latitudeDelta/2)) &gt; self.topLeftCoordinate.latitude) { currentRegion.center.latitude = (topLeftCoordinate.latitude - (currentRegion.span.latitudeDelta/2)); } else if((currentRegion.center.latitude - (currentRegion.span.latitudeDelta/2)) &lt; self.bottomRightCoordinate.latitude) { currentRegion.center.latitude = (bottomRightCoordinate.latitude + (currentRegion.span.latitudeDelta/2)); } else { changeRegionLat = NO; } if(changeRegionLong || changeRegionLat) [self setRegion:currentRegion animated:YES]; } -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{ [scrollView setContentOffset:scrollView.contentOffset animated:YES]; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload