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プロパティは (プロトコルではなく) クラスで定義されますMKPinAnnotationViewMKAnnotation

デリゲート メソッド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 に答える