7.6 Displaying Custom Pins on a Map View

来源:互联网 发布:芒果店淘宝首页模板 编辑:程序博客网 时间:2024/06/03 13:09


自定义pin

- (MKAnnotationView *)mapView:(MKMapView *)mapView

            viewForAnnotation:(id <MKAnnotation>)annotation

{

   MKAnnotationView *result = nil;

   if ([annotation isKindOfClass:[MyAnnotationclass]] == NO)

    {

       return result;

    }

   if ([mapView isEqual:self.myMapView] ==NO)

    {

        /* We want to process this event only for the Map View

         that we have created previously */

       return result;

    }

    /* First typecast the annotation for which the Map View has fired this delegate message */

   MyAnnotation *senderAnnotation = (MyAnnotation *)annotation;

    /* Using the class method we have defined in our custom annotation class, we will attempt to get a reusable identifier for the pin we are about to create */

   NSString *pinReusableIdentifier = [MyAnnotation

                                      reusableIdentifierforPinColor:senderAnnotation.pinColor];

    /* Using the identifier we retrieved above, we will attempt to reuse a pin in the sender Map View */

   MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapViewdequeueReusableAnnotationViewWithIdentifier:pinReusableIdentifier];

   if (annotationView == nil){

        /* If we fail to reuse a pin, then we will create one */

        annotationView =[[MKPinAnnotationViewalloc] initWithAnnotation:senderAnnotation

                                       reuseIdentifier:pinReusableIdentifier];

        /* Make sure we can see the callouts on top of each pin in case we have assigned title and/or subtitle to each pin */

        annotationView.canShowCallout =YES;

    }

   UIImage *pinImage = [UIImageimageNamed:@"aa.jpg"];

    NSLog(@"pingImage = %@",pinImage);

   if (pinImage != nil){

        annotationView.image = pinImage;

    }

    result = annotationView;

   return result;

}


其他的同前一节

输出


0 0
原创粉丝点击