0

I am currently making an app with a map that should focus on a certain location only. I would like the user to not be able to zoom out or pan out of this area so they can keep their focus on the image overlay that i have put over this area.

In order to get the app to start off from the location that i want and not some random map, I used a tutorial from Ray Wenderlich: https://www.raywenderlich.com/425-mapkit-tutorial-overlay-views

How would I acoomplish my task based on the code that is written in the tutorial above? I have completed the tutorial, so I am looking for help in adding any code and identifying where and what kind of code to put.

I found other tutorials on this topic unhelpful because they were for other map types like Google maps or MapBox. The apple website about MapKit and MaximumZ does not help me very much either.

I am a beginner in XCode and Swift, and have only had little bit of experience in Python previously. I was hoping limiting the zoom and user access to parts of the maps would be easier...

override func viewDidLoad() {
  super.viewDidLoad()

  let latDelta = park.overlayTopLeftCoordinate.latitude -
    park.overlayBottomRightCoordinate.latitude

  // Think of a span as a tv size, measure from one corner to another
  let span = MKCoordinateSpanMake(fabs(latDelta), 0.0)
  let region = MKCoordinateRegionMake(park.midCoordinate, span)

  mapView.region = region
}

This is what I have so far for getting the app to startup on the location that I want, using a rectangle that bounds the area that I am looking to restrict the user to.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

The MKMapView has a delegate MKMapViewDelegate. This protocol has a function called:

func mapViewDidChangeVisibleRegion(_:)

This method is called whenever the user scrolls or zooms the map. In this method you can specify the behavior of the map. You can, for instance set a specific region that you want the map to zoom into and specify the maximum level of zoom allowed.

In the function mapViewDidChangeVisibleRegion(_:) you can then check to what latitudeDelta and longitudeDelta the map can zoom into. If the delta's go below or above a certain level you can lock the zooming by setting the region with something like this:

func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {

    if mapView.region.span.latitudeDelta < 0.4 {
        regionSpan = MKCoordinateSpan(latitudeDelta: 0.4, longitudeDelta: 0.5)
        let mapRegion = MKCoordinateRegion(center: self.centerCoordinate, span: self.regionSpan)
        self.trackMapView.setRegion(mapRegion, animated: true)
    }
}
MacUserT
  • 1,760
  • 2
  • 18
  • 30
  • Hi, I tried using the IsZoomEnabled and isZScrollEnabled commands and they did work, but my problem now is that t he user cannot zoom in at all. what can i do if I want the user to be able to zoom in and zoom out anywhere within the set boundary that I set? ie set a minimum zoom level on the map but also turn off the scroll when the user scrolls past a certain spot, but turn it back on when the user is in the boundary? Thanks – Raymond Song Jul 08 '19 at 18:53
  • I copied and pasted the above code into xcode to see if I could get it to work, but it came up with error messages about the ViewController having no member "CentreCoordinate" or the unresolved identifier 'regionSpan'. I have my own coordinates for the Centre Coordinate as shown below, but how would I make the function identify these variables? The following code was pasted into the viewDidLoad and not the actual function: – Raymond Song Jul 09 '19 at 14:38
  • let latDelta = park.overlayTopLeftCoordinate.latitude - park.overlayBottomRightCoordinate.latitude let span = MKCoordinateSpanMake(fabs(latDelta), 0.0) let region = MKCoordinateRegionMake(park.midCoordinate, span) – Raymond Song Jul 09 '19 at 14:38
  • Hi Raymond, the code is an example from what I build. The variablecenterCoordinate is not a MapView property. You have to define that yourself when defining a MKCoordinateRegion. – MacUserT Jul 10 '19 at 12:32