11

我以这种方式向我的地图添加注释:

MyAnnotation *annotationPoint2 = [[MyAnnotation alloc] init];
annotationPoint2.coordinate = anyLocation;
annotationPoint2.title = [NSString stringWithFormat:@"%@", obj];
annotationPoint2.subtitle = @"";  //or set to nil
annotationPoint2.keyValue = [NSString stringWithFormat:@"%@", key];
[mapPins addAnnotation:annotationPoint2];

别针都是红色的,我希望它们都是绿色的。我怎样才能改变颜色?我尝试了以下方法,但它仍然给出了一个红色标记:

annotationPoint2.pinColor = MKPinAnnotationColorGreen;
4

3 回答 3

23
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
  {
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
    annView.pinColor = MKPinAnnotationColorGreen;
    return annView;
  }
于 2012-10-12T14:47:47.847 回答
6

pinColor属性是在MKPinAnnotationView类(不是MKAnnotation协议)中定义的。

MKPinAnnotationView您在viewForAnnotation委托方法中创建一个。如果您尚未实现该委托,则默认情况下您会获得标准的红色引脚。

在该委托方法中,您创建一个实例,MKPinAnnotationView您可以将其设置pinColor为绿色。

于 2012-10-12T13:14:54.773 回答
5

Swift3 是这样的:

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

    let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

    if annotation.title! == "My Place" {

        annotationView.pinTintColor = UIColor.green

    } else {

        annotationView.pinTintColor = UIColor.red
    }


    return annotationView
}
于 2016-10-12T17:54:55.540 回答