【问题标题】:Customise Annotation in MapKit in swiftUI在 swiftUI 中自定义 MapKit 中的注释
【发布时间】:2020-04-25 08:41:07
【问题描述】:

我尝试在 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
    }


}

【问题讨论】:

  • EHAM R06 赢了!主题:如果你想要一个更大的图标使用图像,你需要使图像更大(如:更多像素)。

标签: xcode swiftui mapkit wrapper mapkitannotation


【解决方案1】:

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:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多