How can I zoom a map to fit a polyline

Hi

I have a mapview on which I'm drawing a polyline representing the users journey using location services. After the journey has been saved, and the user reopens it, I'm displaying the route that was taken. I would very much like to zoom the map to fit the entire polyline when the view is loaded. I've had no luck searching Google or the forums.

Does anyone know how I can achive this?

Thanks in advance...

Assume you know the distance of the polyline then calculate a bounding box of said line, the bounding box will the be zoom level.

Thanks MobileTen but can you be more specific please. The polyline(s) may represent thousands of location points and double back on on itself.

I found a couple of older posts a few days ago along the same line (CGRect?) but nothing I could get to work. I gave up and have only today decided to have another go.

I'm new to Swift and struggling with the bugginess of XCode. Seems like something completely unrelated to what I'm tring to do catches me out more times a day than I actually learn something. Very dificult to stay focussed ;o)

Thanks again...

Accepted Answer

I got it working thanks, turns out I was just being a bit dull. Used a slightly different approach (no doubt not the best, but hey it's late):

First get the min and max longitude and latitudes from the location data and then....


// Work out the size of the zoom area.

let c1 = CLLocation(latitude: minLatitude, longitude: minLongitude)

let c2 = CLLocation(latitude: maxLatitude, longitude: maxLongitude)

let zoom = c1.distance(from: c2)

// Establish the centre point for the map.

let location = CLLocationCoordinate2D(latitude: (maxLatitude+minLatitude)*0.5, longitude: (maxLongitude+minLongitude)*0.5)

// Create a region and fit the map to it.

let region = MKCoordinateRegionMakeWithDistance(location, zoom, zoom)

map.setRegion(region, animated: true)


Please feel free to correct me if this is not the best approach, I'm new to Swift and appreciate any pointers.


Thanks again...

How can I zoom a map to fit a polyline
 
 
Q