在swiftUI 2中将MapMarker添加到MapKit [英] adding a MapMarker to MapKit in swiftUI 2

查看:108
本文介绍了在swiftUI 2中将MapMarker添加到MapKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的

 导入CoreLocation导入MapKit结构标记:可识别的{设id = UUID()var位置:MapMarker}struct DemoView:查看{@State私有var区域= MKCoordinateRegion(中心:CLLocationCoordinate2D(纬度:38.8977,经度:-77.0365),跨度:MKCoordinateSpan(latitudeDelta:0.5,经度Delta:0.5))let markers = [Marker(位置:MapMarker(坐标:CLLocationCoordinate2D(纬度:38.8977,经度:-77.0365),色调:.red))]var body:some View {Map(coordinateRegion:$ region,showsUserLocation:true,注解项目:标记){标记位置} .edgesIgnoringSafeArea(.all)}} 

I am trying to use the new Mapkit SwiftUI view and I am able to show a map with a certain region but I can't figure how to show a Map Marker.

This is what I have:

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

Does anyone know how to add location1 to the Map? I found this but I have not been able to make it work

解决方案

Here is a simple demo. Tested with Xcode 12 / iOS 14

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

这篇关于在swiftUI 2中将MapMarker添加到MapKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆