首页 > 解决方案 > 如何更改 MapKit 中的 markerTintColor 而不更改 Swift 中的默认位置引脚?

问题描述

我有 MKMarkerAnnotationView 来更改地图上大头针的颜色。

func mapView(_ MapView:MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{

    let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")

    view.markerTintColor = .blue

    return view

}

但是,当我启动我的应用程序时,我的默认位置标记变为。如何在不更改此标记的情况下更改引脚?查看位置的代码也很简单

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    self.MapView.showsUserLocation = true
}

感谢您的回答!:)

标签: iosswiftannotationsmapkitswift4

解决方案


您可以检查注释是否是这样的用户位置:

func mapView(_ MapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")
    view.markerTintColor = .blue
    return view
}

推荐阅读