1

In iOS I have an MKMapView and CLLocationManager to update the heading and location. No matter what I do, I can't get the map view to stop zooming in tightly on the user's location. I have annotations on the map that I want visible at all times, and I want the heading to update as the user spins the device. This is working using mapView.setRegion, but after a bit, the map locks at the north and no longer rotates. Below is the CLLocationManager code I'm using:

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    if data != nil{
        setRegion(locationManager?.location?.coordinate.latitude)!, longitude: (locationManager?.location?.coordinate.longitude)!), data: data!)
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if !regionSet && data != nil{
        setRegion(location: CLLocationCoordinate2D(latitude: (locationManager?.location?.coordinate.latitude)!, longitude: (locationManager?.location?.coordinate.longitude)!), data: data!)
        regionSet = true
    }
}

setRegion calculates the extents of the map to zoom on and sets the setRegion function appropriately.

Below is setRegion. I calculate the zoomWindowDegrees variable when I load in the annotations by finding the maximum lat/long for the annotations and the use that in the function.

private func setRegion(location:CLLocationCoordinate2D, towers:[Tower]){
        let center = location

        let coordinateSpan = MKCoordinateSpan(latitudeDelta: zoomWindowDegrees, longitudeDelta: zoomWindowDegrees)
        let region = MKCoordinateRegion(center: center, span: coordinateSpan)
        mapView.setRegion(region, animated: false)
}
PJ Slauta
  • 53
  • 5
  • Can you show us the code for your `setRegion` function? – Mike Mertsock Dec 07 '17 at 21:55
  • I edited the question to include the setRegion function. – PJ Slauta Dec 07 '17 at 22:58
  • What is `zoomWindowDegrees`? Is it a constant value? Do you calculate a new value for it each time you call `setRegion`? – Mike Mertsock Dec 08 '17 at 14:37
  • @MikeMertsock it’s calculated when the annotations are created. – PJ Slauta Dec 08 '17 at 16:46
  • 1
    Consider setting the `camera` property of the map view rather than the `region`. MKMapCamera allows you to specify the center coordinate, zoom level, and heading independently, and gives you the ability to preserve or adjust any existing map rotation. Check [this Stack Overflow post](https://stackoverflow.com/a/26910828/795339) and also [this one](https://stackoverflow.com/q/21034409/795339) for some ideas. – Mike Mertsock Dec 08 '17 at 17:03

0 Answers0