17

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 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.

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.

I've researched alot on this topic, but I didn't found a nice solution.

Any help is appreciated!

Best Regards, Christian

Edit: This is our solution: 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.

LimitedMapView.h:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface LimitedMapView : MKMapView <UIScrollViewDelegate>{

}

@property (nonatomic, assign) CLLocationCoordinate2D topLeftCoordinate;
@property (nonatomic, assign) CLLocationCoordinate2D bottomRightCoordinate;


@end

LimitedMapView.m:

#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 > 0.002401f || [self region].span.longitudeDelta > 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)) < self.topLeftCoordinate.longitude) {

        currentRegion.center.longitude = (topLeftCoordinate.longitude + (currentRegion.span.longitudeDelta/2));

    } else if((currentRegion.center.longitude + (currentRegion.span.longitudeDelta/2)) > self.bottomRightCoordinate.longitude) {

        currentRegion.center.longitude = (bottomRightCoordinate.longitude - (currentRegion.span.longitudeDelta/2));

    } else {

        changeRegionLong = NO;

    }

    // LATITUDE    
    if((currentRegion.center.latitude + (currentRegion.span.latitudeDelta/2)) > self.topLeftCoordinate.latitude) {

        currentRegion.center.latitude = (topLeftCoordinate.latitude - (currentRegion.span.latitudeDelta/2));

    } else if((currentRegion.center.latitude - (currentRegion.span.latitudeDelta/2)) < 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
cweinberger
  • 3,358
  • 1
  • 13
  • 30
  • Thanks for updating with solution. I just wanted to limit max zoom, and ran into a bit of a problem w/ using just scrollViewDidZoom: To anyone else out there, make sure you set your new span to LESS than the if check span otherwise the mapview will get stuck at max zoom. – Andrew Jun 17 '11 at 15:38
  • A better written solution for swift is there http://stackoverflow.com/a/31799617/2814964 – cl3m Sep 23 '15 at 14:15

3 Answers3

14

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];
  }
}
shannoga
  • 19,000
  • 20
  • 99
  • 163
David van Dugteren
  • 3,238
  • 8
  • 31
  • 47
  • 1
    I should mention the above code also has a limit on the zoom level. – David van Dugteren May 27 '11 at 03:00
  • For anyone else trying to get this to work, this is definitely the way to go. – Undistraction Jun 13 '12 at 11:34
  • This turned into an infinite recursion to me. I had to unset the map delegate from the NIB, and set the delegate in `viewDidLoad` after setting the initial region. – Mazyod Dec 29 '12 at 04:50
  • 5
    Thanks for this solution. Small note - If anyone else has trouble with this in future I had to change abs(x) to fabs(x) as lon/lats are doubles and abs(x) returns an int. This doesn't seem to matter with the values you have as they are so large, but I was working with much smaller regions! – bean Apr 03 '13 at 14:12
  • This crashes my app. And how did you get this value (mapView.region.span.latitudeDelta > 15.589921 )?? 15.589921?? And should mapView be self.mapView? – coolcool1994 Apr 07 '14 at 18:07
  • 2
    While this has definitely worked for me (and you have received my upvote) it is overly verbose and there is one too many absolute value function. (Do use fabs(x) as @bean pointed out). I've created a more legible gist for anyone interested: https://gist.github.com/Alp-Phone/e11cca67e77285566d4d – Moritz May 27 '14 at 14:20
6

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.

Community
  • 1
  • 1
Vincent Guerci
  • 14,094
  • 4
  • 47
  • 55
  • Hmm.. I think this delegate will fire when I start moving the Map. Unfortunalety at this moment I don't know what the user will do with the Map (could be a valid zoom/scroll gesture). And the `-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated` fires too late. Or am I totally wrong with this?! – cweinberger Apr 16 '11 at 11:09
  • 1
    added another solution that might help ;) – Vincent Guerci Apr 16 '11 at 12:44
  • that's not so nice, just dirty "hacking" a bit some apis that are not open enough for certain uses... aah if *only apple* and google were still *friends* we might have a featured and stable `MapKit`... – Vincent Guerci Apr 16 '11 at 14:37
  • in this case, anything which will work and look ok (for the end-user), is a nice solution to me. but you're right... there definitely should be a method included to limit the viewable region :/ – cweinberger Apr 16 '11 at 14:42
  • Thanks for your hints. Added my solution to the question. For the moment it's ok for us :). – cweinberger Apr 22 '11 at 12:59
0

I know this is a pretty old question, but to avoid the infinite loop you could try something like this: https://stackoverflow.com/a/4126011/1234011

Basically set a flag to prevent the callback from firing again if it was you who set the value.

  • Ken
Community
  • 1
  • 1
Ken Koch
  • 426
  • 3
  • 12