ios - 更改 MKmapView 中图钉的颜色会将新图钉图像以及当前位置变成旧图钉图像?

标签 ios swift annotations mkmapview uicolor

我正在尝试将一组引脚的颜色更改为蓝色,并将另一组引脚的颜色更改为红色。我在这里关注了一个问题,这让我了解到引脚确实会改变颜色。而且我当前的位置也变成了一个图钉。此外,图钉也从新 Logo 变为旧 Logo (实际上是图钉的图像)。有没有办法让图钉保持像新 Logo /图像一样,并且还可以正常显示我当前的位置,即蓝色脉冲点。以及将一组引脚更改为蓝色,而其余引脚保持红色?

请帮忙! 下面是我的代码:

class MyPointAnnotation : MKPointAnnotation {
var pinTintColor: UIColor?
}

class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var map: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    map.delegate = self

    let anx = MyPointAnnotation()
    anx.coordinate = CLLocationCoordinate2D(latitude: -36.56795, longitude: 176.56432)
    anx.pinTintColor = .red

    let any = MyPointAnnotation()
    any.coordinate = CLLocationCoordinate2D(latitude: -36.54322, longitude: 176.43221)
    any.pinTintColor = .blue

    map.addAnnotation(anx)
    map.addAnnotation(any)
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "myAnnotation") as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation")
    } else {
        annotationView?.annotation = annotation
    }

    if let annotation = annotation as? MyPointAnnotation {
        annotationView?.pinTintColor = annotation.pinTintColor
    }

    return annotationView
}
}

最佳答案

But also my current location turns into a pin.

mapView:viewFor...方法中添加以下代码:

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

关于ios - 更改 MKmapView 中图钉的颜色会将新图钉图像以及当前位置变成旧图钉图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49142228/

相关文章:

ios - SQLIte 比较日期时间

swift 核心数据 : Unable to section tableView using sectionNameKeyPath with custom function

ios - 快速更新 Realm 数据库中的值

java - 调用 REST API 的自定义 Java 注释

ios - 如何通过单击 iOS 中的特定注释来获取值?

ios - 没有这样的文件或目录,但文件存在 Xcode

ios - 我如何执行这些动画 View 转换?

swift - 使用结构进行转换时的对称性。什么是最佳实践?

ios - 如何让 Mapbox 注释显示当前位置信息

ios - 如何在导航栏 ios 上重叠图像