swift 在MKMapView上显示日/夜叠加

7lrncoxx  于 5个月前  发布在  Swift
关注(0)|答案(1)|浏览(59)

我需要在显示与世界上的地方是在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

bksxznpy

bksxznpy1#

由于我主要处理SwiftUI,而MapKit只有在iOS 17之后才能在SwiftUI中完全可用,所以我将向您展示我的想法(但需要SwiftUI并且目标必须是iOS 17)。
我从CLLocationCoordinate2D中创建了一个数组来在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()
}

字符串


的数据

相关问题