2011-10-30 4 views
0

私の地図は、 "会場"のために赤いピンを表示する必要があります。マップに多色のピンを作成するにはどうすればよいですか?

問題は、両方のピンが同じ色で表示されることです。

コードは以下の通りです:

マイpinAnnotationクラスは次のようになります。

#import <MapKit/MapKit.h> 


@interface mapPinAnnotation : NSObject <MKAnnotation> {  
    NSString *title; 
    NSString *subtitle; 
    NSString *pinType; // this string will be set to either "Venue" or "Parking" 
    CLLocationCoordinate2D coordinate; 
} 


@property (nonatomic, retain) NSString *title, *subtitle; 
@property (nonatomic) CLLocationCoordinate2D coordinate; 
@property (nonatomic, retain) NSString *pinType; 


@end 

これは実装です:ここでは

#import "mapPinAnnotation.h" 

@implementation mapPinAnnotation 

@synthesize coordinate; 
@synthesize pinType; 
@synthesize title, subtitle; 

-(id) initWithCoordinate: (CLLocationCoordinate2D) c { 
    coordinate = c; 
    return self; 
}  

@end 

はピンを設定する方法である - 注意して私はグローバルに宣言された "tempPin"変数を使用して、そのピンを "viewForAnnotation"メソッドに渡すことができますが、これは問題がどこにあるかと思います:

-(void) dropThePin { 
    CLLocationCoordinate2D location = mapView.userLocation.coordinate; 

    location.latitude = latitude; 
    location.longitude = longitude;   

    if(pinAnnotation != nil) { 
     [mapView removeAnnotation:pinAnnotation]; 
     [pinAnnotation release]; 
     pinAnnotation = nil; 
    } 

    // Create (alloc/init) a Pin, set its Title & Subtitle, and add/place it: 
    pinAnnotation = [[mapPinAnnotation alloc] initWithCoordinate:location]; 
    pinAnnotation.pinType = @"VENUE"; 

    tempPin = pinAnnotation; 

    [pinAnnotation setTitle: @"Some Stadium"]; 
    [pinAnnotation setSubtitle: @"123 Main St."]; 

    [mapView addAnnotation:pinAnnotation]; 


    // Set-Up of 2nd. Pin: 
    location.latitude = 12.34567; 
    location.longitude = -23.45678; 

    pinAnnotation.pinType = @"PARKING"; 
    if(parkingLotPin != nil) { 
     [mapView removeAnnotation:parkingLotPin]; 
     [parkingLotPin release]; 
     parkingLotPin = nil; 
    } 

    // Create (alloc/init) a Pin, set its Title & Subtitle, and add/place it: 
    parkingLotPin = [[mapPinAnnotation alloc] initWithCoordinate:location]; 

    tempPin = pinAnnotation; 
    [parkingLotPin setTitle: @"Another Venue"]; 
    [parkingLotPin setSubtitle: @"789 S. Broad Street"]; 

    [mapView addAnnotation:parkingLotPin]; 
    [parkingLotPin release]; 

} 

最後に、ここでは「viewForAnnotation」メソッドです:

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

    MKPinAnnotationView *thePin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; 

    if (tempPin.pinType == @"VENUE") { 
     thePin.pinColor = MKPinAnnotationColorGreen; 
     NSLog(@"pin-type = %@", tempPin.pinType); 
    } else { 
     thePin.pinColor = MKPinAnnotationColorPurple; 
     NSLog(@"pin-type = %@", tempPin.pinType); 
    } 

    thePin.animatesDrop=TRUE; 
    thePin.canShowCallout = YES; 
    thePin.calloutOffset = CGPointMake(-5, 5); 
    return thePin; 
} 

問題は、両方のピンが同じ色で表示さという点です。

提案がありますか?

+0

私はあなたが' isEqualToStringを使用したいと思います'.pinType'です。 – msgambel

+0

試しました(isEqualToString) - 動作しません:-( – sirab333

+0

最初の文字列が実際に文字列を比較し、2番目の文字列は比較しないので、文字列を 'isEqualToString'メソッドで比較しなければなりません。あなたのコードには他にも問題があるかもしれませんが、確かにそれらの問題の1つです。また、最初のピン注釈を追加した後にリリースしてから、後で新しいものを割り当ててから何かを設定してください。あなたの '.pinType = @" PARKING "' * BEFORE *新しいピンを割り当てようとしている場合は、 – msgambel

答えて

1

変更してみてください:

if (tempPin.pinType == @"VENUE") { 

に:あなたが比較されたときに代わりに `` == `の:

if ([((mapPinAnnotation*)annotation).pinType isEqualToString:@"VENUE"]) { 
+1

'(mapPinAnnotation *)'にする必要がありますが、基本的には正しいです。 @ sirab333、 'viewForAnnotation'デリゲートメソッドはビューが必要なアノテーションへの参照を提供します。その参照を使用する必要があります。 ivarを使ってマップビューがそのメソッドを呼び出す注釈を確実に伝えることはできません。 – Anna

+0

訂正してくれてありがとう、さらに詳しい説明。 – Joe

0
if ([tempPin.pinType isEqualToStirng:@"VENUE"]) { 
    thePin.pinColor = MKPinAnnotationColorGreen; 
    NSLog(@"pin-type = %@", tempPin.pinType); 
} else { 
    thePin.pinColor = MKPinAnnotationColorPurple; 
    NSLog(@"pin-type = %@", tempPin.pinType); 
} 

Also look at this

+0

@Joe - あなたは正しいです正確に問題 - 私は私のオリジナルの投稿で言及したと思う。だから問題は残っています。これをどうやって解決しますか? :-) 'viewForAnnotation'メソッドをどのように変更して、各ピンに適切な色を表示するのですか? – sirab333

関連する問題