2011-09-16 52 views
1

我想添加一個自定義視圖,其中有四個文本字段和五個圖像,點擊地圖視圖的圖釘。我在.m文件中使用了以下代碼。如何在點擊地圖視圖的別針時添加自定義視圖?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    appDelegate = (Yelp_OnTheWayAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [email protected]"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) 
     { 
      pinAnnotation = [[[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]; 
} 

如何在自來水龍頭上添加自定義視圖以及如何訪問自定義視圖類?

回答

0

here您將得到更好的指導...

+0

鏈接返回404 –

0

你可以做以下的事情:

  1. 在耐磨層視圖爲MKMap
  2. 繪製創建覆蓋。
  3. 爲自定義視圖添加動畫。
相關問題