3

如何使用 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()
    }
}

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

4

1 回答 1

3

更新您的代码:

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)






    }
}

预习

于 2019-09-16T19:52:56.333 回答