开发者

How to add a custom view when tap on pin of map view?

I want to add a custom view in which I have four text field and five images with tap on pin of map view. I have use following code in .m file.

- (void)viewDidLoad
{
    [super viewDidLoad];

    appDelegate = (Yelp_OnTheWayAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.title=@"Map";
    [aiv startAnimating]; 
    label.text=[NSString stringWithFormat:@"%d places along your route",[appDelegate.busines_Aray count]];
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    [mapView setDelegate:self];
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = appDelegate.start_lat;
    region.center.longitude = appDelegate.start_long;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;  
    [mapView setRegion:region animated:YES];

    dm=[[DisplayMap alloc]init];
    for(int i=0;i<[Place_Object.aray_cord count];i++)
    {
        CLLocationCoordinate2D coord; 
        coord.latitude=[[Place_Object.aray_lat objectAtIndex:i] floatValue];
        coord.longitude=[[Place_Object.aray_long objectAtIndex:i] floatValue];
        dm=[[DisplayMap alloc]init];
        dm.coordinate=coord;
        dm.title=[appDelegate.lB objectAtIndex:i ];
        dm.pinID=i;
        [mapView addAnnotation:dm]; 
        NSLog(@"%@,%i at %f and %f",dm.title,dm.pinID,dm.coordinate.latitude,dm.coordinate.longitude);
    }
    routeOverlayView = [[UICRouteOverlayMapView alloc] initWithMapView:mapView];
    diretions = [UICGDirections sharedDirections];
    diretions.delegate = self;
    if (diretions.isInitialized)
    {
        [self update];
    }   
}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;     
    }
    static NSString *identifier = @"RoutePinAnnotation";
    if ([annotation isKindOfClass:[UICRouteAnnotation class]])
    {
        MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if(!pinAnnotation)
        {
            pinA开发者_如何学编程nnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
        }

        if ([(UICRouteAnnotation *)annotation annotationType] == UICRouteAnnotationTypeStart)
        {
            pinAnnotation.pinColor = MKPinAnnotationColorGreen;
        } else if ([(UICRouteAnnotation *)annotation annotationType] == UICRouteAnnotationTypeEnd) {
            pinAnnotation.pinColor = MKPinAnnotationColorPurple;
        } else {
            pinAnnotation.pinColor = MKPinAnnotationColorPurple;
        }
        pinAnnotation.animatesDrop = YES;
        pinAnnotation.enabled = YES;
        pinAnnotation.canShowCallout = YES;
        return pinAnnotation;
   } 
   static NSString * const kPinAnnotationIdentifier = @"PinIdentifier";
   MKAnnotationView *draggablePinView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
   UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
   if (draggablePinView) {
       draggablePinView.annotation = annotation;
   } else {
       // Use class method to create DDAnnotationView (on iOS 3) or built-in draggble MKPinAnnotationView (on iOS 4).
       draggablePinView = [DDAnnotationView annotationViewWithAnnotation:annotation reuseIdentifier:kPinAnnotationIdentifier mapView:self.mapView];
       // draggablePinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

       DisplayMap *ano = annotation;
       NSLog(@"%i",ano.pinID);
       button.tag=ano.pinID;
       draggablePinView.canShowCallout = YES;
       draggablePinView.rightCalloutAccessoryView = button;

       if ([draggablePinView isKindOfClass:[DDAnnotationView class]]) {
           // draggablePinView is DDAnnotationView on iOS 3.
       } else {
           // draggablePinView instance will be built-in draggable MKPinAnnotationView when running on iOS 4.
       }
   }        
   return draggablePinView;
}


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{   
    if(DV_Object == nil)
        DV_Object = [[DetailView alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];

    BusinessData *business_datas = [appDelegate.busines_Aray objectAtIndex:control.tag];

    DV_Object.business = business_datas;

    [self.navigationController pushViewController:DV_Object animated:YES];
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
}

How add custom view on pin tap and how access my custom view class?


From here you will get a better guidance...


You can do the following things:

  1. Create overlay for the MKMap
  2. Draw on the over-layer your view.
  3. Add animation for the custom view.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜