2013-05-29 72 views
1

我有一個引腳在地圖上顯示來自數據庫的JSON字符串的數據。我設置了引腳的ID(在自定義類中定義),但是當按下詳細信息泄漏按鈕時,我需要將ID傳遞給一個操作。詳細信息披露發送自定義註釋數據

然後它會打開與該引腳上的一些信息,一個新的視圖(通過一個數據庫,它會查找該行ID和返回數據,不知道IM如何去做到這一點,只是還沒有)

這是代碼:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    [spinner stopAnimating]; 

    // json parsing 
    results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 

    NSMutableArray * locations = [[NSMutableArray alloc] init]; 
    CLLocationCoordinate2D location; 
    CustomAnnotation * myAnn; 

    NSArray *pins = mapView.annotations; 

    if ([pins count]) 
    { 
     [mapView removeAnnotations:pins]; 
    } 

    /* loop through the array, each key in the array has a JSON String with format: 
    * title <- string 
    * strap <- string 
    * id <- int 
    * date <- date 
    * lat <- floating point double 
    * long <- floating point double 
    * link <- string 

    */ 
    int i; 


    for (i = 0; i < [results count]; i++) { 
     //NSLog(@"Result: %i = %@", i, results[i]); 
     //NSLog(@"%@",[[results objectAtIndex:i] objectForKey:@"long"]); 

     myAnn = [[CustomAnnotation alloc] init]; 
     location.latitude = (double)[[[results objectAtIndex:i] objectForKey:@"lat"] doubleValue]; 
     location.longitude = (double)[[[results objectAtIndex:i] objectForKey:@"long"] doubleValue]; 
     myAnn.coordinate = location; 
     myAnn.title = [[results objectAtIndex:i] objectForKey:@"title"]; 
     myAnn.subtitle = [[results objectAtIndex:i] objectForKey:@"strap"]; 
     myAnn.pinId = [[results objectAtIndex:i] objectForKey:@"id"]; 

     NSLog(@"id: %i", myAnn.pinId); 

     [locations addObject:myAnn]; 

     //NSLog(@"%i", [[results objectAtIndex:i] objectForKey:@"lat"]); 
    } 


    [self.mapView addAnnotations:locations]; 

} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ 
    static NSString *s = @"ann"; 
    MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s]; 
    if (!pin) { 
     pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s]; 
     pin.canShowCallout = YES; 
     //pin.image = [UIImage imageNamed:@"pin.png"]; 

     pin.calloutOffset = CGPointMake(0, 0); 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [button addTarget:self 
        action:@selector(viewDetails) forControlEvents:UIControlEventTouchUpInside]; 
     pin.rightCalloutAccessoryView = button; 

    } 
    return pin; 
} 

-(void)viewDetails{ 

    NSLog(@"press"); 

} 

回答

3

在我看來,你希望檢測哪個註釋被選中或者它的輔助視圖被點擊。

使用下面的方法

-(void) mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 

可用於檢測該標註輕擊動作。但是,而不是設置按鈕的標籤,標籤的mkannotationView,你會爲您提供註解的顯示

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

一些最好的教程是 http://www.raywenderlich.com/21365/introduction-to-mapkit-in-ios-6-tutorial

+0

嗨!感謝你,我appriciate它。第一個方法是否替換上面代碼中的viewForAnnotation方法?感謝您的鏈接,我會看看那裏的項目 - 看起來像一個偉大的網站! –

+0

如果你在我的文章中談到第一種方法,那麼它不會取代viewforannotation。 viewForAnnotation是爲了不同的目的,calloutAccessoryControlTapped是檢測附件控件,在你的mkannotationview的標註上,當你點擊標註本身時出現。請通過鏈接建議,肯定會幫助您更好地理解MKMapView及其代表的使用邏輯和用法。 –

相關問題