5

I am making an app for iOS6 using mapkit. I want to limit the boundaries of the map only to a specific region/country. Is there a way to do this?

The problem has already been discussed here: how can I limit the map area to only one country in iOS? with the only difference that the possible solution is for ios5.

Community
  • 1
  • 1

1 Answers1

3

You need to make a CLLocationCorrdinate2D then use that to make a region, then set your map view to that region. Here is an example of a map set to ireland:

CLLocationCoordinate2D ireland = CLLocationCoordinate2DMake(53.317749,-7.959643);
[self.mapView setRegion: MKCoordinateRegionMakeWithDistance(ireland, 500000, 30000)];
[self.mapView setMapType: MKMapTypeStandard];

it will look something like this depending on what you're testing on: enter image description here those last 2 numbers of RegionMakeWithDistance are the lat/long of the span in meters. The CLLocation you made is the center point of the map. If you want to limit just to that region, you are gonna want to turn scrolling and zooming off for the map view.

lciamp
  • 675
  • 1
  • 9
  • 19
  • Thank you for the comment. In your solution it's not possible to zoom in within the region, isn't it? I need to set boundaries of the map as well as zoom in. – user1895675 Dec 11 '12 at 22:50
  • [self.mapView setZoomEnabled: YES]; then you can zoom in. However, it doesn't stop them from zooming out. – lciamp Dec 11 '12 at 22:53
  • you can try setting [self.mapView setZoomEnabled: YES]; and [self.mapView setScrollEnabled: NO]; then they can zoom in and out but the Coord you made will always be the center of the map. ETA: you're gonna want them to be able to scroll when they zoom in. – lciamp Dec 11 '12 at 22:54
  • i want to code this thing: map is opened as full region. then user can zoom in, scrolling to all sides, but when user reaches the boundary, one can not scroll over the boundary. Will your suggestions help me in coding this? – user1895675 Dec 11 '12 at 23:01
  • The boundary is basically the region you set up. I do not know how to prevent someone from scrolling out side of it. – lciamp Dec 11 '12 at 23:10
  • I was looking around, apparently you can implement – mapView:regionDidChangeAnimated: to the delegate and then reset the region when the user scrolls out. – lciamp Dec 11 '12 at 23:16
  • I believe that I need to use this algorythm: when a gesture scrolling the map is done some delegate gets the information about the senter the map and via it about the boundaries. If user scrolls to far, we stop scrolling to this direction cancelling the movement. Is is possible to analyze the position of the map DURING the gesture and not only when the movement is done? – user1895675 Dec 11 '12 at 23:45
  • 1
    This works, I just tried it. It doesn't stop you from scrolling out but when you do it goes back to your region, it also allows scrolling and zooming in your region. http://stackoverflow.com/questions/5680896/ios-how-to-limit-the-mapview-to-a-specific-region it's the second answer – lciamp Dec 12 '12 at 00:05
  • may I ask, did you try it in ios5 or ios6? – user1895675 Dec 12 '12 at 00:28
  • constantly analyzing the movement of the map by the delegate can slow down the application. did you notice any slow downs? also, when you scroll to the boundary - if you scroll further - a)you simply can't scroll further OR b)you can scroll out of the region a little bit (as long as your gesture allows) and then the map jumps back ? – user1895675 Dec 12 '12 at 00:40
  • 1
    You can scroll past a little bit then it bounces back. As far as I know, there is no way to just make it stop scrolling when it hits a boundary.(you might be able to do this with overlays but its something i've never even tried.) Also, moving around in the mapview only updates when you move around in it. Constantly monitoring the users location slows down the app and kills the battery life. Scrolling out of a region is not the same thing as the user entering a new region. – lciamp Dec 12 '12 at 00:52