RinaredTap
RinaredTap

Reputation: 5

Display day / night overlay on MKMapView

I need help in displaying an overlay with where in the world is night and day on MKMapView.

The idea is to display where is night a black opaque color, and where is day nothing. I've been trying to use MKOverlay, but had no luck.

Minimal reproducing code provided.

struct OurMapView: UIViewRepresentable {
    
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        mapView.showsUserLocation = true
        mapView.mapType = .standard // Set the map type to satellite
        return mapView
    }

    func updateUIView(_ uiView: MKMapView, context: Context) {
       
    }
}

EDIT: I am aiming to do something similar, but without toggles, just display where is day and night: https://appadvice.com/app/day-night-map/741375889

Upvotes: 0

Views: 181

Answers (1)

SomeUser
SomeUser

Reputation: 56

Since I mainly deal with SwiftUI, and MapKit is only fully possible in SwiftUI since iOS 17, I'll show you my idea (but requires SwiftUI and target must be iOS 17).

I created an array from CLLocationCoordinate2D to display a polygon on the map.

import SwiftUI
import MapKit

struct ContentView: View {

    //Array for the MapPolygon
    let nightArea: [CLLocationCoordinate2D] = [
        CLLocationCoordinate2D(latitude: 90.0, longitude: -74.0060),
        CLLocationCoordinate2D(latitude: 90.0, longitude: -74.0060),
        CLLocationCoordinate2D(latitude: 35.6895, longitude: 139.6917),
        CLLocationCoordinate2D(latitude: -33.8688, longitude: 151.2093),
        CLLocationCoordinate2D(latitude: -34.6037, longitude: -58.3816),
    ]

    var body: some View {
        Map() {
            MapPolygon(coordinates: nightArea)
                 .foregroundStyle(.black.opacity(0.60))
        }
        
    }
}

#Preview {
    ContentView()
}

Here is a small screenshot of Xcode Preview

Upvotes: 1

Related Questions