开发者

How to add title to a pin in MKMapView?

开发者 https://www.devze.com 2023-04-03 16:40 出处:网络
I have code for drop pin. But i want to show title bar as in image above. H开发者_开发知识库ow add images of star and review image?use this delegate method

How to add title to a pin in MKMapView?

I have code for drop pin. But i want to show title bar as in image above. H开发者_开发知识库ow add images of star and review image?


use this delegate method

 -(MKAnnotationView*)mapView:(MKMapView)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
      // If you are showing the users location on the map you don't want to change it
      MKAnnotationView *view = nil;
      if (annotation != mapView.userLocation) {
        // This is not the users location indicator (the blue dot)
        view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
        if (!view) {
          // Could not reuse a view ...

          // Creating a new annotation view, in this case it still looks like a pin
          view = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease];
          view.canShowCallOut = YES; // So that the callout can appear

          UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someName"]];
          myImageView.frame = CGRectMake(0,0,31,31); // Change the size of the image to fit the callout

          // Change this to rightCallout... to move the image to the right side
          view.leftCalloutAccessoryView = myImageView;
          [myImageView release], myImageView = nil;
        }
      }
      return view;
    }


It's the same as with custom cells in TableView. You just need to create new subclass of MKAnnotationView, and draw the view what you want.


Otherwise, you can try this tutorial to get custom callouts: http://dev.tuyennguyen.ca/?p=298


To do so, you have to put images in the annotation to make it customized, i think this article http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1 by James Rantanen can help you out.


    - (MKAnnotationView *)mapView:(MKMapView *)mapViewTmp viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if(annotation == mapView.userLocation)
            return nil;
        MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewTmp dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
        if (pinView ==nil) {
            pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Pin"];
            {

                if([addedAnns containsObject:annotation]){
                    MyAnnotation *a = [addedAnns objectAtIndex:[addedAnns indexOfObject:annotation]];
                    UIImage * image = [UIImage imageNamed:a.pinImage];
                    UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
                    imageView.frame=[pinView bounds];
                    [pinView addSubview:imageView];
                    //[imageView release];
                    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDeta`enter code here`ilDisclosure];
                    pinView.rightCalloutAccessoryView.tag = a.iAnnId;
                    [(UIButton *)pinView.rightCalloutAccessoryView addTarget:self action:@selector(openSpot:) forControlEvents:UIControlEventTouchUpInside];


                    pinView.enabled = YES;
                    pinView.centerOffset = CGPointMake(0,-15);
                    pinView.calloutOffset = CGPointMake(-8,0);
                    pinView.canShowCallout = YES;
                }
            }
            pinView.animatesDrop = YES;     
        } 
        return pinView; // we cant release Or Auto release this object. Due To it will use futher
    }





// The left accessory view to be used in the standard callout.
                    @property (retain, nonatomic) UIView *leftCalloutAccessoryView;

                    // The right accessory view to be used in the standard callout.
                    @property (retain, nonatomic) UIView *rightCalloutAccessoryView;

As you can see I am adding button to rightCalloutAccessoryView. Similarly you can Add Images To It.


You can use your custom MKAnnotationView.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号