1

我在地图上显示了来自数据库的 JSON 字符串数据的图钉。我设置了 pin 的 id(在自定义类中定义),但在按下详细信息披露按钮时我需要将 ID 传递给操作。

然后它将打开一个新视图,其中包含有关该引脚的一些信息(通过数据库,它将查找行 ID 并返回数据,尚不确定我将如何做到这一点)

这是代码:

-(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");

}
4

1 回答 1

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

于 2013-05-29T11:38:53.557 回答