2012-07-13 81 views
1

請幫助我,我堅持這個MapView很多引腳,如何區分每個引腳?

我有一個MapView並根據該MapView的小標籤。在MapView上,我有很多引腳(MKPinAnnotationView),我知道如何使用數組設置每個引腳的標題和副標題,但我不知道如何區分每個引腳。我的意思是當用戶點擊一個引腳時,標籤將顯示引腳引腳的標題。

下面是我的一些代碼:

這是我將引腳定義:

@implementation PlacePin 

@synthesize coordinate,title,subtitle; 
@synthesize nTag; 

- (id)initWithLocation:(CLLocationCoordinate2D)coord{ 

    self = [super init];  
    if (self) {   
     coordinate = coord;  
    } 
    return self;  
} 

-(void)dealloc{ 
    [title release]; 
    [subtitle release]; 
    [super dealloc]; 
} 
@end 

這是我proccess從服務器結果:

- (void) resultCheck { 

NSString *strUrl = [NSString stringWithFormat:@"ServerAddress.com"]; 

NSLog(@"MapView - resultCheck: url: %@ ", strUrl); 
NSURL *url   = [ NSURL URLWithString:strUrl]; 

//get the result from server 
NSString *result = [NSString stringWithContentsOfURL:url]; 

NSLog(@"MapView - resultCheck: result: %@", result); 


NSDictionary *dictionary = [result JSONValue]; 
NSLog(@"MapView - resultCheck: dictionary: %@", dictionary); 





//process the JSON, get two parameters: xPos, yPos 
NSDictionary *value1 = [dictionary valueForKey:@"result"]; 
NSDictionary *value2 = [value1 valueForKey:@"post"]; 



NSArray *arrXPos = [value2 valueForKey:@"xPos"];   //array of xPos 
NSArray *arrYPos = [value2 valueForKey:@"yPos"];   //array of yPos 
self.arrName  = [value2 valueForKey:@"name"];   //array of name 
self.arrPlaceInfor = [value2 valueForKey:@"place_info"]; 



NSLog(@"MapView - resultCheck: value1: %@",value1); 
NSLog(@"MapView - resultCheck: value2: %@",value2); 
NSLog(@"MapView - resultCheck: value3: %@",arrXPos); 
NSLog(@"MapView - resultCheck: value4: %@",arrYPos); 



//get the xPos and yPos 
for (int i = 0 ; i < [value2 count]; i++) { 

    //display the place depended on the xPos and yPos 

    CLLocationCoordinate2D location; 

    location.latitude = [[ NSString stringWithFormat:@"%@",[arrXPos objectAtIndex:i]] doubleValue]; 
    location.longitude = [[ NSString stringWithFormat:@"%@",[arrYPos objectAtIndex:i]] doubleValue]; 


    PlacePin *mapPoint = [[PlacePin alloc] initWithLocation:location]; 
    //set the title and subtitle of the pin depending on the result from server 
    mapPoint.title  = [ NSString stringWithFormat:@"%@",[self.arrName objectAtIndex:i]]; 
    mapPoint.subtitle = [ NSString stringWithFormat:@"%@",[self.arrPlaceInfor objectAtIndex:i]]; 

    [mapView addAnnotation:mapPoint]; 
    [mapPoint release]; 


    mapPoint = nil; 



} 

}

這是我處理引腳和設置標籤 - 我卡在哪裏:(

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(NA, 4_0){ 

    self.labelShortIntro.text = @"1111111111111"; 


} 

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

    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; 
    annView.pinColor    = MKPinAnnotationColorPurple; 
    annView.animatesDrop   = TRUE; 
    annView.canShowCallout   = YES; 
    annView.calloutOffset   = CGPointMake(-5, 5); 
    return annView; 
} 

回答

0

didSelectAnnotationView代表:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    NSArray *selectedAnnotations = mapView.selectedAnnotations; 

    for(PlacePin *ann in selectedAnnotations) 
    { 
     NSLog(@"%@",ann.title); 
     self.labelShortIntro.text=ann.title; 
    } 
} 

我希望這個代碼將幫助你。

+0

非常感謝你,userar和WrightsCS。這麼快,如此正確!你真的幫助我走出困境! :) – acerpc 2012-07-13 06:52:44

1
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 

    PlacePin *selectedPin  =  view.annotation; 

    NSLog(@"Title = %@ Subtitle = %@",selectedPin.title,selectedPin.subtitle); 

    self.labelShortIntro.text =  [NSString stringWithFormat:@"Title = %@ Subtitle = %@",selectedPin.title,selectedPin.subtitle]; 

} 
+0

短暫而容易理解。非常感謝你,納文。 :) – acerpc 2012-07-13 07:10:17