Swift 3.0 为 MapView 设置注释图钉颜色

标签 swift annotations android-mapview mkannotationview

我在为我的 map 注释设置图钉颜色时遇到问题。我在我的 MapView viewcontroller 中有一个函数,它从另一个 View Controller 的数组中提取,并且根据类型的情况,我想要 map View 的不同引脚颜色。我不确定如何将引脚颜色信息添加到此 switch 语句中的注释。我对注释的理解相当薄弱,因此非常感谢任何解释,而不是解决方案本身。

class ColorPointAnnotation: MKPointAnnotation {
    var pinColor: UIColor

    init(pinColor: UIColor) {
        self.pinColor = pinColor
        super.init()
    }
}


    func add(newLocation location_one:[String:Any]) {

    let momentaryLat = (location_one["latitude"] as! NSString).doubleValue
    let momentaryLong = (location_one["longitude"] as! NSString).doubleValue

    var annotation = MKPointAnnotation()

    switch location_one["type"] {
        case "Tomorrow":
            print("The pin color is red")
            annotation = ColorPointAnnotation(pinColor: UIColor.red)
        case "Next Week":
            print("The pin color is green")
            annotation = ColorPointAnnotation(pinColor: UIColor.green)
        default:
            print("The pin color is purple")
            annotation = ColorPointAnnotation(pinColor: UIColor.purpleColor)
    }

    annotation.title = location_one["title"] as? String
    annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)


    DispatchQueue.main.async {
        self.map.addAnnotation(annotation)
    }

    self.map.centerCoordinate = annotation.coordinate

}


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

    //        if (annotation is MKUserLocation) {
    //            return nil
    //        }

    let identifier = "pinAnnotation"
    var annotationView = map.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView


    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true
        let colorPointAnnotation = annotation as! ColorPointAnnotation
        annotationView?.pinTintColor = colorPointAnnotation.pinColor

    }
    //      else {
    //            annotationView?.annotation = annotation
    //
    //        }
    //        map.showAnnotations(map.annotations, animated: true)
    return annotationView
}

最佳答案

您需要将 switch 语句移动到 viewForAnnotation 委托(delegate)方法中。在这里,当您退回图钉时,您可以自定义颜色,然后将其退回。

像这样:

        annotation.pinColor = MKPinAnnotationColorGreen;

更新的答案:

您可以子类化 MKPointAnnotation,并添加一个存储注释类型的属性。

当您在 add 方法中创建注释时,将属性设置为任何类型的引脚。

现在在viewForAnnotation方法中,mapkit会给出你的类型的注解。查看设置的属性并确定返回哪个颜色图钉。

如果您想查看一些代码,请告诉我。

关于Swift 3.0 为 MapView 设置注释图钉颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44014932/

相关文章:

swift - swift 2.0 中过滤方法的第二个参数

swift - 在 Swift 中推进范围内的所有元素?

swift - SCNNode旋转多轴

swift - 如何使用 Cloud Firestore 索引查询数据

java - CsvToBeanBuilder 无法将 CSV 数据转换为对象

java - 对命名查询使用 if else 语句?

java - JPA 带注释的对象与常规值对象一样

android - MapView MultiTouch zoom (Pinching) 不可靠且困惑

java - 如何以编程方式移动 mapView (osmdroid)

android - GoogleMap - animateCamera() 在 fragment 恢复后不执行任何操作