swift 在MKMapView上显示日/夜叠加

7lrncoxx  于 2024-01-05  发布在  Swift
关注(0)|答案(1)|浏览(172)

我需要在显示与世界上的地方是在MKMapView白天和黑夜覆盖的帮助。
我们的想法是显示哪里是晚上一个黑色不透明的颜色,哪里是白天什么都没有。我一直试图使用MKOverlay,但没有运气。
提供最小的复制代码。

  1. struct OurMapView: UIViewRepresentable {
  2. func makeUIView(context: Context) -> MKMapView {
  3. let mapView = MKMapView()
  4. mapView.showsUserLocation = true
  5. mapView.mapType = .standard // Set the map type to satellite
  6. return mapView
  7. }
  8. func updateUIView(_ uiView: MKMapView, context: Context) {
  9. }
  10. }

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

bksxznpy

bksxznpy1#

由于我主要处理SwiftUI,而MapKit只有在iOS 17之后才能在SwiftUI中完全可用,所以我将向您展示我的想法(但需要SwiftUI并且目标必须是iOS 17)。
我从CLLocationCoordinate2D中创建了一个数组来在Map上显示一个多边形。

  1. import SwiftUI
  2. import MapKit
  3. struct ContentView: View {
  4. //Array for the MapPolygon
  5. let nightArea: [CLLocationCoordinate2D] = [
  6. CLLocationCoordinate2D(latitude: 90.0, longitude: -74.0060),
  7. CLLocationCoordinate2D(latitude: 90.0, longitude: -74.0060),
  8. CLLocationCoordinate2D(latitude: 35.6895, longitude: 139.6917),
  9. CLLocationCoordinate2D(latitude: -33.8688, longitude: 151.2093),
  10. CLLocationCoordinate2D(latitude: -34.6037, longitude: -58.3816),
  11. ]
  12. var body: some View {
  13. Map() {
  14. MapPolygon(coordinates: nightArea)
  15. .foregroundStyle(.black.opacity(0.60))
  16. }
  17. }
  18. }
  19. #Preview {
  20. ContentView()
  21. }

字符串


的数据

展开查看全部

相关问题