2

文字列 0 、1 および 2 を含む xml を解析します。

//参考までに 0 = 緑 1 = 赤 2 = 紫

プロパティである以下の変数を含む MKAnnotation を確認するクラスがあります。

CLLocationCoordinate2D  coordinate;
NSString            *title;
NSString            *subtitle;
MKPinAnnotationColor pinColor;

このクラスは MyAnnotation という名前です

マップ ビューの viewDidLoad で、for ループを実行して、以下のように解析されたデータを反復処理します (locationArray はこのデータを保持し、すべての情報を適切に引き出します。

 for (int i = 0; i < locationArray.count; i++) {
    myAnnotation =[[MyAnnotation alloc] init];

    NSString *thePointName = [[locationArray objectAtIndex:i]xmlName];
    NSString *theAddress = [[locationArray objectAtIndex:i]xmlAddress];

    NSString *latString = [[locationArray objectAtIndex:i]xmlLat];
    NSString *lonString = [[locationArray objectAtIndex:i]xmlLon];

//これは、ピンの色を表す前述の 0、1、または 2 つの文字列を引き出す文字列です。 poinType は文字列として保持されます。

    pointType = [[locationArray objectAtIndex:i]xmlType];

    double theLatitude = [latString doubleValue];
    double theLongtitude = [lonString doubleValue];

    userLocation.latitude=theLatitude;
    userLocation.longitude=theLongtitude;

    myAnnotation.coordinate=userLocation;
    myAnnotation.title=[NSString stringWithFormat:@"%@", thePointName];
    myAnnotation.subtitle=[NSString stringWithFormat:@"%@", theAddress];

    //I log that the points are actually giving either of the colors and if so set MyAnnotation class to the pincolor 

    NSLog(@"Points Color %@", pointType);
    if ([pointType isEqualToString:@"0"]){
        myAnnotation.pinColor = MKPinAnnotationColorGreen;
    }else if ([pointType isEqualToString:@"1"]){
        myAnnotation.pinColor = MKPinAnnotationColorRed;
    }else if ([pointType isEqualToString:@"2"]) {
        myAnnotation.pinColor = MKPinAnnotationColorPurple;
    }

    [mapView addAnnotation:myAnnotation];

    }

MKAnnotationViewビューで、以下を実行します

  - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id     <MKAnnotation>)annotation

 {

// if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                  initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;

    //set pin color to the correct colour
    if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {
    pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (myAnnotation.pinColor = MKPinAnnotationColorRed) {
    pinView.pinColor = MKPinAnnotationColorRed;

     }

    else if (myAnnotation.pinColor = MKPinAnnotationColorPurple){
    pinView.pinColor = MKPinAnnotationColorPurple;
    }



    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
                action:@selector(showDetails:)
      forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;


    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"Profile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;

return pinView;
  }

上記の方法も試しましたが、ピンの色を設定していません。他のすべては大丈夫です!

    //set pin color to the correct colour
    if (pointType isEqualToString:@"0") {
    pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (pointType isEqualToString:@"1") {
    pinView.pinColor = MKPinAnnotationColorRed;

    }

    else if (pointType isEqualToString:@"2"){
    pinView.pinColor = MKPinAnnotationColorPurple;
    }
4

1 に答える 1

3

のこのコードviewForAnnotation:

if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {

次の 2 つの理由で機能しません。

  1. 同等性をチェックするためではなく、割り当て用の単一の等号を使用しています。等しいかどうかを確認するには、2 つの等号を使用する必要があります。ただし、これは理由#2である主な問題を解決しません...

  2. コードは、myAnnotationこのデリゲート メソッドの外部で設定された変数の値をチェックしています。が設定されている for ループと同期してデリゲート メソッドが呼び出されるという保証はありませんmyAnnotationviewForAnnotationを呼び出した直後に が呼び出されると想定しないでくださいaddAnnotation。ズームまたはパンの後にマップ ビューでアノテーションを再度表示する必要がある場合は、同じアノテーションに対してデリゲート メソッドを複数回呼び出すこともできます。

annotation代わりに、デリゲート メソッドに渡されるパラメーターを使用する必要があります。これは、デリゲート メソッドの現在の呼び出しでマップ ビューがビューを必要とする注釈への参照です。

annotationパラメータは一般的に として型指定されるため、id<MKAnnotation>最初にそれをカスタム クラスにキャストする必要があります。その後、任意のカスタム プロパティにアクセスできます。

//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
    MyAnnotation *currentAnn = (MyAnnotation *)annotation;

    if (currentAnn.pinColor == MKPinAnnotationColorGreen) {
        pinView.pinColor = MKPinAnnotationColorGreen;
    }

    else if (currentAnn.pinColor == MKPinAnnotationColorRed) {
        pinView.pinColor = MKPinAnnotationColorRed;

    }

    else if (currentAnn.pinColor == MKPinAnnotationColorPurple) {
        pinView.pinColor = MKPinAnnotationColorPurple;
    }
}

またはさらに簡単:

//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
    MyAnnotation *currentAnn = (MyAnnotation *)annotation;        
    pinView.pinColor = currentAnn.pinColor;
}

rightButton(関係ありませんが、表示されないのにコードがタイトルを設定しているのはなぜですか?)

于 2012-11-23T03:33:19.353 に答える