在 MKMapView 上显示日/夜叠加

问题描述 投票:0回答:1

我需要帮助在 MKMapView 上显示世界上哪里是黑夜和白天的叠加层。

这个想法是显示哪里是夜晚,哪里是黑色不透明颜色,哪里是白天,什么都没有。我一直在尝试使用 MKOverlay,但没有成功。

提供最少的复制代码。

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) {
       
    }
}

编辑:我的目标是做类似的事情,但没有切换,只显示白天和黑夜的位置:https://appadvice.com/app/day-night-map/741375889

swift swiftui mapkit mkmapview
1个回答
0
投票

由于我主要处理 Swift UI,并且自 iOS 17 以来 MapKit 只能在 Swift UI 中完全实现,我将向您展示我的想法(但要求 Swift UI 和目标必须是 iOS 17) 我从 CLLocationCooperative2D 创建了一个数组来在地图上显示多边形。

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

© www.soinside.com 2019 - 2024. All rights reserved.