【问题标题】:Add single pin to Mapkit with SwiftUI使用 SwiftUI 将单个引脚添加到 Mapkit
【发布时间】:2019-09-16 17:16:53
【问题描述】:

如何使用 Xcode 11 GM / SwiftUI 在我的地图上添加一个简单的图钉。

我的代码如下(这里显示了以坐标为中心的地图)但我只想显示其他坐标的一个图钉。

import SwiftUI
import MapKit

struct ContentView: UIViewRepresentable {
    func makeUIView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }

    func updateUIView(_ view: MKMapView, context: Context) {
        let coordinate = CLLocationCoordinate2D(
            latitude: 34.011_286, longitude: -116.166_868)
        let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        view.setRegion(region, animated: true)



    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

如果有任何建议,我将不胜感激,谢谢。

【问题讨论】:

    标签: ios mapkit swiftui


    【解决方案1】:

    更新您的代码:

    struct ContentView: UIViewRepresentable {
    
    
        func makeUIView(context: Context) -> MKMapView {
            MKMapView(frame: .zero)
        }
    
        func updateUIView(_ view: MKMapView, context: Context) {
    
    
            // 1 
            view.mapType = MKMapType.standard // (satellite)
    
            // 2
            let mylocation = CLLocationCoordinate2D(latitude: -6.863190,longitude: -79.818250)
    
            // 3
            let coordinate = CLLocationCoordinate2D(
                latitude: -6.864138, longitude: -79.819634)
            let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
            let region = MKCoordinateRegion(center: coordinate, span: span)
            view.setRegion(region, animated: true)
    
            // 4
            let annotation = MKPointAnnotation()
            annotation.coordinate = mylocation
    
            annotation.title = "My Location"
            annotation.subtitle = "Visit us soon"
            view.addAnnotation(annotation)
    
    
    
    
    
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-12
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多