Swift 3.0为MapView设置注释接点颜色
我无法为地图批注设置pin颜色。我的MapView viewcontroller中有一个函数,它从另一个视图控制器的阵列中提取,并且根据类型的情况,我希望地图视图具有不同的管脚颜色。我不确定如何将pin颜色信息添加到此switch语句中的注释。我对注释的理解相当薄弱,因此非常感谢任何解释,而不是解决方案本身Swift 3.0为MapView设置注释接点颜色,swift,annotations,android-mapview,mkannotationview,Swift,Annotations,Android Mapview,Mkannotationview,我无法为地图批注设置pin颜色。我的MapView viewcontroller中有一个函数,它从另一个视图控制器的阵列中提取,并且根据类型的情况,我希望地图视图具有不同的管脚颜色。我不确定如何将pin颜色信息添加到此switch语句中的注释。我对注释的理解相当薄弱,因此非常感谢任何解释,而不是解决方案本身 class ColorPointAnnotation: MKPointAnnotation { var pinColor: UIColor init(pinColor: U
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委托方法中。在这里,当您返回pin时,您可以自定义颜色,然后将其返回 像这样:
annotation.pinColor = MKPinAnnotationColorGreen;
最新答复: 可以将MKPointAnnotation子类化,并添加存储注释类型的属性 在add方法中创建注释时,请将该属性设置为管脚的任何类型 现在在viewForAnnotation方法中,mapkit将给出您类型的注释。查看set属性并确定要返回的颜色pin
如果您想查看一些代码,请告诉我。我根据我在添加MKPointAnnotation子类(请参见编辑)的基础上阅读的文档添加了一些代码。如何在add方法中引用这个子类?