在 swiftUI 的 MapKit 中自定义注释

Customise Annotation in MapKit in swiftUI

我尝试在 MapKit 和 SwiftUI 中自定义我的注释

根据下面的代码,我在地图中搜索特定坐标 (coord) 并显示我的自定义注释。

1) 我尝试增加 UIimage 的大小,因为它太小了(见附图)并改变了颜色,知道怎么做吗?

2)应用程序启动后在地图中只显示图标,点击图标后出现注释,知道如何在不点击的情况下立即显示我的注释吗?

3)现在在注释中我设法显示标题和副标题,如何显示坐标

struct MapView: UIViewRepresentable {

    let Mm : MapManager
    let coord = CLLocationCoordinate2D(latitude: 52.28792, longitude: 4.73415327)

    class Coordinator: NSObject, MKMapViewDelegate {
        var parent : MapView

        init(_ parent: MapView) {
            self.parent = parent
        }

        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "TESTING NOTE")
            annotationView.canShowCallout = true
            annotationView.image = UIImage(systemName: "location.circle")

            return annotationView
        }


    }
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: Context) -> MKMapView {

    let view =  MKMapView(frame: .zero)
        Mm.georeverseCoordinate(coord) { (pin) in

                if let pinOK = pin {

                    view.removeAnnotation(pinOK)
                    view.mapType = MKMapType.satellite
                    let span = MKCoordinateSpan(latitudeDelta: 0.04, longitudeDelta: 0.04)
                    let region = MKCoordinateRegion(center: self.coord, span: span)
                    view.setRegion(region, animated: true)
                    view.delegate = context.coordinator
                    view.addAnnotation(pinOK)
                }

            }
        return view
    }

    func updateUIView(_ view: MKMapView, context: Context) {

    }

}

地图管理器

class MapManager: NSObject, CLLocationManagerDelegate {

    static let shared = MapManager()




    func georeverseCoordinate(_ coord: CLLocationCoordinate2D , closure:  @escaping (Pin?) -> Void) {

        let location = CLLocation(latitude: coord.latitude, longitude: coord.longitude)


        let geocoder = CLGeocoder()

        geocoder.reverseGeocodeLocation(location) { (arrayResponse, error) in
            if let errorTest = error {
                debugPrint(errorTest.localizedDescription)
                closure(nil)
                return
            }

            if let arrayPins = arrayResponse {

                if let valorePinArray = arrayPins.first {

                    debugPrint(valorePinArray.locality!)
                    debugPrint(valorePinArray.isoCountryCode!)

                    let pin = Pin(title: valorePinArray.locality!, subtitle: valorePinArray.isoCountryCode!, coordinate: valorePinArray.location!.coordinate)

                    closure(pin)

                }
                else { closure(nil) }
            }
            else { closure(nil) }
        }
    }
}

引脚型号

class Pin:NSObject, MKAnnotation {

    var title : String?
    var subtitle : String?
    var coordinate : CLLocationCoordinate2D
    var color: UIColor?

    init(title: String?, subtitle: String?, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate
    }


}

1) - 似乎有一个错误使得使用 tintColor 和特定渲染模式更改 SF 符号的颜色变得困难,但有一个解决方法也可以轻松更改符号的大小。

mapView:viewFor annotation中添加以下内容:

annotationView.canShowCallout = true    
annotationView.image = UIImage(systemName: "location.circle")?.withTintColor(.systemGreen, renderingMode: .alwaysOriginal)
let size = CGSize(width: 40, height: 40)
annotationView.image = UIGraphicsImageRenderer(size:size).image {
     _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
}

2) - 要在将注释添加到地图后立即显示标注,请使用 selectAnnotation

view.addAnnotation(pinOK)
view.selectAnnotation(pinOK, animated: true)

3) - 坐标在构造注释时可用,因此您只需更改 Pin 中的 init class:

init(title: String?, subtitle: String?, coordinate: CLLocationCoordinate2D) {
    self.coordinate = coordinate
    self.title = "\(coordinate.latitude) : \(coordinate.longitude)"
    self.subtitle = subtitle
}