I have a function that is adding a geodesic polyline on a great circle track between two points on a map. The issue I am facing is dynamically changing the size of the map to include both the start position and the end position.
Any tips on what I am missing here as I am setting the setVisibleMapRect???
Code below is for Melbourne Airport to Brisbane Airport with the result
var body: some View {
MapView()
.onAppear {
self.mapRoute.toggle()
self.updateMapOverlayViews()
}
.frame(height: 256)
.cornerRadius(25)
.padding()
}
func addRoute(latitudeStart: Double, longitudeStart: Double, latitudeEnd: Double, longitudeEnd: Double) {
let c1 = CLLocation(latitude: latitudeStart, longitude: longitudeStart)
let c2 = CLLocation(latitude: latitudeEnd, longitude: longitudeEnd)
let zoom = c1.distance(from: c2)
//Draws a MKGeodesicPolyline between the two points.
var coordinates = [c1.coordinate, c2.coordinate]
let geodesicPolyline = MKGeodesicPolyline(coordinates: &coordinates, count: 2)
mapView.addOverlay(geodesicPolyline)
let location = CLLocationCoordinate2D(latitude: (latitudeStart+latitudeEnd) * 0.5, longitude: (longitudeStart+longitudeEnd) * 0.5)
let region = MKCoordinateRegion(center: location, latitudinalMeters: zoom, longitudinalMeters: zoom)
let adjustRegion = mapView.regionThatFits(region)
mapView.setRegion(adjustRegion, animated: true)
mapView.setVisibleMapRect(geodesicPolyline.boundingMapRect, edgePadding: UIEdgeInsets.init(top: 80.0, left: 100.0, bottom: 100.0, right: 100.0), animated: false)
}
func updateMapOverlayViews() {
mapView.removeAnnotations(mapView.annotations)
mapView.removeOverlays(mapView.overlays)
if mapRoute { addRoute(latitudeStart: -37.6690, longitudeStart: 144.8410, latitudeEnd: -27.3942, longitudeEnd: 153.1218) }
}
Result:
