ios - 在 swiftUI 2 中将 MapMarker 添加到 MapKit

标签 ios swiftui mapkit ios14

我正在尝试使用新的 Mapkit SwiftUI view我可以显示某个区域的 map ,但我不知道如何显示 Map Marker .
这就是我所拥有的:

import SwiftUI
import MapKit

struct ContentView: View {
    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
    var location1 =  MapMarker(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), tint: .red)
    
    
    var body: some View {
        Map(coordinateRegion: $region, showsUserLocation: true).edgesIgnoringSafeArea(.all)
    }
}
有谁知道怎么加location1到 map ?我找到了 this但我无法让它发挥作用

最佳答案

这是一个简单的演示。使用 Xcode 12/iOS 14 测试
demo

import CoreLocation
import MapKit

struct Marker: Identifiable {
    let id = UUID()
    var location: MapMarker
}

struct DemoView: View {

    @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))

    let markers = [Marker(location: MapMarker(coordinate: CLLocationCoordinate2D(latitude: 38.8977, longitude: -77.0365), tint: .red))]


    var body: some View {
        Map(coordinateRegion: $region, showsUserLocation: true, 
          annotationItems: markers) { marker in
            marker.location
        }.edgesIgnoringSafeArea(.all)
    }
}

关于ios - 在 swiftUI 2 中将 MapMarker 添加到 MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62989013/

相关文章:

ios - MKMapView 与零高度约束斗争 - map View 的 edgeInsets?

ios - 跨 iOS 版本稳定的 String 哈希值?

ios - 如何为 didReceiveRemoteNotification 生成组合发布器?

ios - 如何在 Mapview 中使用动画自定义 Pin(gif 图像)?

ios - 如何设置MKMapView区域以缩放到userLocation的精度?

ios - 如果应用程序在 iOS 9 上处于后台,则不会出现推送通知

ios - 粒子过滤器 iOS 实现失败

ios - 使组合直通发布者不那么全局化

swift - 如何在 SwiftUI 中正确使用 NavigationLink

ios - 为什么 ABCreateStringWithAddressDictionary 没有给出详细名称?