【问题标题】:How to change markerTintColor in MapKit without changing default location pin in Swift?如何更改 MapKit 中的 markerTintColor 而不更改 Swift 中的默认位置引脚?
【发布时间】:2018-08-28 13:10:26
【问题描述】:

我有 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
}

感谢您的回答! :)

【问题讨论】:

    标签: ios swift annotations mapkit swift4


    【解决方案1】:

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

    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
    }
    

    【讨论】:

      【解决方案2】:

      在您的方法中,检查注解对象是否为MKUserLocation 的实例。如果是,则返回 nil 以保留标准的用户位置注释视图。

      mapView(_:viewFor:) 的文档对此进行了解释。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-28
        • 1970-01-01
        • 1970-01-01
        • 2014-12-18
        • 1970-01-01
        • 1970-01-01
        • 2021-04-19
        • 1970-01-01
        相关资源
        最近更新 更多