【问题标题】:How to show the title on my Pin Annotation and make it clickable to perform some action?如何在我的 Pin Annotation 上显示标题并使其可点击以执行某些操作?
【发布时间】:2011-07-22 02:01:35
【问题描述】:

在我的 MapView 中,我在某些位置放置了图钉,现在我想在这些图钉注释上显示一些标题并使其可点击,以便在点击时我可以推送另一个视图。 请帮忙!!!!

【问题讨论】:

    标签: iphone objective-c mkmapview


    【解决方案1】:

    这是代码sn-p:

    使用此方法的自定义注释视图

    - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if (annotation == mapView.userLocation) return nil;
    
        MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];
        if (pin == nil)
        {
            pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"asdf"] autorelease];
        }
        else
        {
            pin.annotation = annotation;
        }
        pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pin.pinColor = MKPinAnnotationColorRed;
        pin.animatesDrop = YES;
        [pin setEnabled:YES];
        [pin setCanShowCallout:YES];
        return pin;
    
    }
    

    点击按钮时调用的委托方法

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
    
        //DetailPg=[[BookMarksDetail alloc]initWithNibName:@"BookMarksDetail" bundle:nil];
        //DetailPg.selectRest =[view.annotation title]; //In addition, to get the Title of the AnnotationView.
        //DetailPg.m=1;
        //[self.navigationController pushViewController:DetailPg animated:YES];
        //[DetailPg release];
    
    }
    

    【讨论】:

      【解决方案2】:

      Apples Docs 中都提供了这个问题的答案,我建议您仔细阅读它们。但是,要进行调用,您需要做的就是设置 title 属性。

      对于按钮等你需要使用 MapView Delegate 方法:

      - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
      
      _pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
      

      Apple 建议重复使用注释,这是您需要考虑的事项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多