Hi,
I’m trying to use an image as an overlay using MapKit, but I’m pretty new to using MapKit and I’m struggling to find anything helpful on the internet, as most of what’s out there relates to UIKit, which I’ve never used and a lot of the terms are unfamiliar to me. I’ve worked out how to draw a rectangle around the area where I’d like the image to be (code for this is below), but I can’t work out how to add an image instead of a polygon. Any help would be much appreciated!
import MapKit
struct MapPractice4: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
// Corner coordinates
let lat1 = 64.667394
let lat2 = 64.672321
let long1 = -1.681905
let long2 = -1.669555
let rectangleCoordinates = [CLLocationCoordinate2D(latitude: lat1, longitude: long1),CLLocationCoordinate2D(latitude: lat1, longitude: long2), CLLocationCoordinate2D(latitude: lat2, longitude: long2), CLLocationCoordinate2D(latitude: lat2, longitude: long1), CLLocationCoordinate2D(latitude: lat1, longitude: long1)]
let rectanglePolygon = MKPolygon(coordinates: rectangleCoordinates, count: rectangleCoordinates.count)
// Create map with overlay
let map = MKMapView()
map.delegate = context.coordinator
map.addOverlay(rectanglePolygon)
// Set visible region
map.setVisibleMapRect(rectanglePolygon.boundingMapRect, animated: true)
return map
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
static func dismantleUIView(_ uiView: MKMapView, coordinator: ()) {
}
// MARK: Coordinator
func makeCoordinator() ->Coordinator {
return Coordinator()
}
class Coordinator: NSObject, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
renderer.lineWidth = 5
renderer.strokeColor = .blue
return renderer
}
}
}