0

While driving I record the different coordinates during the drive. If I drive between two points that is generally in a straight line, then the map span of the following code works well but if I drive around the block then the map span doesn't show the route. I can generally see that since I am using the first and last coordinate this would be why. The region is then put into a snapshot to generate an image of the map. What I'm struggling with is being able to use all coordinates to calculate the needed span.

//calculate region
let span = MKCoordinateSpan.init(latitudeDelta: fabs(startCoord.latitude - endCoord.latitude) * 1.6, longitudeDelta: fabs(endCoord.longitude - startCoord.longitude) * 1.6)
let resd = CLLocationCoordinate2D.init(latitude: startCoord.latitude - (startCoord.latitude - endCoord.latitude) * 0.5, longitude: startCoord.longitude + (endCoord.longitude - startCoord.longitude) * 0.5)

let options = MKMapSnapshotter.Options()
options.scale = UIScreen.main.scale
options.region = MKCoordinateRegion.init(center: resd, span: span);
options.size = CGSize(width: 600, height: 450) //map size
options.showsBuildings = true
options.showsPointsOfInterest = true

I found this bit of code on another question

func regionFor(coordinates coords: [CLLocationCoordinate2D]) -> MKCoordinateRegion {
    var r = MKMapRect.null

    for i in 0 ..< coords.count {
        let p = MKMapPoint(coords[i])

        r = r.union(MKMapRect(x: p.x, y: p.y, width: 0, height: 0))
    }

    return MKCoordinateRegion(r)
}

But I couldn't get it to work to show the circle either. Anyone have an idea of how I could do this?

Micah Montoya
  • 757
  • 1
  • 8
  • 24

1 Answers1

0

A region span is a rectangle. Finding a bounding rectangle is a simpler problem than finding a circle that fully encloses a set of points.

Just set up max and min latitude and longitude variables that start with values that are out of range in the opposite direction, loop through your points, and when a points co-ord is >max or <min, replace that value. When you're done, use the min-max latitude and min-max longitude values to define your region span.

Duncan C
  • 128,072
  • 22
  • 173
  • 272