[SOLVED] MyLocations: why create a region from a region for annotations?

When working out the map areas that needs to be displayed for the [MKAnnotation] the approach is…

func region(for annotations: [MKAnnotation]) -> MKCoordinateRegion {
	// maths to work out top-left & bottom-right
	let extraSpace = 1.1  //border around region
	let span = MKCoordinateSpan( 
	      latitudeDelta: abs(topLeft.latitude - bottomRight.latitude) * extraSpace,
	      longitudeDelta: abs(topLeft.longitude - bottomRight.longitude) * extraSpace)
	let region = MKCoordinateRegion(center: center, span: span)
return mapView.regionThatFits(region)

All makes sense until the last line. We’ve already instantiated ‘region’ which is a region based on a centre and a span to encompass all the annotations. Therefore why do we return mapView.regionThatFits(region) rather than just region?


SOLUTION: I did what I should have done in the first place: checked the documentation. In case anyone else is wondering, the mapView.regionThatFits(region) takes the required region and works out the best region for the mapView you are using to display that region, as its unlikely that the shape of your region will match the shape of your mapView view. Obvious really…! :blush:

1 Like

Thank you for providing the solution to your issue - much appreciated! :]

This topic was automatically closed after 166 days. New replies are no longer allowed.