如何在地图注释视图中找到 pin id?

发布于 2024-11-06 07:01:10 字数 2348 浏览 3 评论 0原文

如何找到被点击的 pin 的 pin id(即 pin 标题和副标题的详细信息)?

我用它来显示引脚注释。此代码在视图中确实加载:

[resultCoordinate addObjectsFromArray:[sqlClass return_Coordinate]];

if ([resultCoordinate count])
{
    for (int i =0; i < [resultCoordinate count]; i++) 
    {
        dict  = [resultCoordinate objectAtIndex:i];

        MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
        region.center.latitude = [[dict objectForKey:@"Lati"] floatValue];
        region.center.longitude = [[dict objectForKey:@"Longi"] floatValue];
        region.span.longitudeDelta = 0.2f;
        region.span.latitudeDelta = 0.2f;

        NSString *fishName = [dict objectForKey:@"Fish"];
        fishName = [fishName stringByAppendingString:@"  "];

        NSString *fishWeight = [dict objectForKey:@"Weight"];
        fishWeight = [fishWeight stringByAppendingString:@" kg"];


        fishName = [fishName stringByAppendingString:fishWeight];

        MapAnnotation *ann = [[MapAnnotation alloc] init];

        ann.title = fishName;
        ann.subtitle = [dict objectForKey:@"DateTime"];

        ann.coordinate = region.center;
        [mapView addAnnotation:ann];
    }
}

并在委托中:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;

    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
    pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    pinView.pinColor = MKPinAnnotationColorPurple;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = infoButton;
    [defaultPinID release];

    return pinView;
}

并单击信息按钮:

-(void)infoButtonPressed:(id)sender
{
    recordCatch = [[RecordCatchDetails alloc] initWithNibName:@"RecordCatchDetails" bundle:nil];
    [self.view addSubview:recordCatch.view] ;
}

其中记录捕获是新的类对象。

How can I find out the pin id (i.e. the details of pin title and subtitle) of pin that was clicked?

I have this to show pins annotations. This code is in view did load:

[resultCoordinate addObjectsFromArray:[sqlClass return_Coordinate]];

if ([resultCoordinate count])
{
    for (int i =0; i < [resultCoordinate count]; i++) 
    {
        dict  = [resultCoordinate objectAtIndex:i];

        MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
        region.center.latitude = [[dict objectForKey:@"Lati"] floatValue];
        region.center.longitude = [[dict objectForKey:@"Longi"] floatValue];
        region.span.longitudeDelta = 0.2f;
        region.span.latitudeDelta = 0.2f;

        NSString *fishName = [dict objectForKey:@"Fish"];
        fishName = [fishName stringByAppendingString:@"  "];

        NSString *fishWeight = [dict objectForKey:@"Weight"];
        fishWeight = [fishWeight stringByAppendingString:@" kg"];


        fishName = [fishName stringByAppendingString:fishWeight];

        MapAnnotation *ann = [[MapAnnotation alloc] init];

        ann.title = fishName;
        ann.subtitle = [dict objectForKey:@"DateTime"];

        ann.coordinate = region.center;
        [mapView addAnnotation:ann];
    }
}

and in delegate:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;

    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
    pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    pinView.pinColor = MKPinAnnotationColorPurple;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = infoButton;
    [defaultPinID release];

    return pinView;
}

and on click of info button:

-(void)infoButtonPressed:(id)sender
{
    recordCatch = [[RecordCatchDetails alloc] initWithNibName:@"RecordCatchDetails" bundle:nil];
    [self.view addSubview:recordCatch.view] ;
}

Where record catch is new class object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2024-11-13 07:01:10

当注释获取选定的地图视图时,会调用其委托中的 mapView:didSelectAnnotationView: 方法,您可以覆盖此方法以获得所需的内容。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
 MKAnnotation *selectedAnnotation = view.annotation // This will give the annotation.
 if([selectedAnnotation.title isEqualToString:@"yourTitle"])
 {
  // Do something
 }
}

如果您想获得附件控制的操作,请单击使用

-(void)mapView:(MKMapView *)mapView annotationView:(MKPinAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
// get annotation details here.
}

When annotation gets selected map view calls mapView:didSelectAnnotationView: method in its delegate, you can override this to get what you want.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
 MKAnnotation *selectedAnnotation = view.annotation // This will give the annotation.
 if([selectedAnnotation.title isEqualToString:@"yourTitle"])
 {
  // Do something
 }
}

If you want to get the action for accessory control click make use of

-(void)mapView:(MKMapView *)mapView annotationView:(MKPinAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
// get annotation details here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文