2010-01-14 10 views
5

Tôi có một MKMapView triển khai bằng các dòng mã nguồn (vị trí của tôi là một viên đạn màu xanh, của một người khác là chân tím):MKMapView -> hiển thị một nút cho vị trí của tôi

- (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id <MKAnnotation>)annotation { 
    if (annotation == mapViewLocal.userLocation) { 
     mapViewLocal.userLocation.title = @"Test"; 
     [mapViewLocal setRegion:MKCoordinateRegionMakeWithDistance(mapViewLocal.userLocation.coordinate, 1000, 1000) animated:YES]; 
     return nil; 
    } 

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewLocal dequeueReusableAnnotationViewWithIdentifier:@"Pin"]; 
     if(pinView == nil) { 
      pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease]; 
      pinView.pinColor = MKPinAnnotationColorPurple; 
      pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      pinView.animatesDrop = NO; 
      pinView.canShowCallout = YES; 
     } else { 
      pinView.annotation = annotation; 
     } 
     return pinView; 
    } 

Các chân tím có một nút tiết lộ chi tiết, nhưng chú thích của tôi không có. Làm thế nào tôi có thể thiết lập một nút như vậy?

Và đó là phương pháp mà tôi có thể làm somethin một nút được pressd:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 

Làm thế nào tôi có thể phân biệt giữa vị trí của tôi và tất cả của một người khác, bởi vì tôi cần một xử lý khác nhau. Có một đại biểu khác hay tôi phải thực hiện một số mệnh đề if?

Trả lời

4

trong didUpdateToLocation ghi của bạn một cái gì đó giống như

AddressAnnotation *myAnnotation = [[AddressAnnotation alloc] initWithCoordinate:currentLocation]; 
       myAnnotation.title = @"You are here"; 
       [self.mapView addAnnotation:myAnnotation]; 

And Then

Something Giống như

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
      // try to dequeue an existing pin view first 
     static NSString* annotationIdentifier = @"annotationIdentifier"; 

     NSString *titlestr = annotation.title; 

     MKPinAnnotationView* pinView = (MKPinAnnotationView *) 
     [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 
    MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] 
              initWithAnnotation:annotation reuseIdentifier:nil] autorelease]; 
     if (!pinView) 
     { 
      // if an existing pin view was not available, create one 
      // MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] 
                //initWithAnnotation:annotation reuseIdentifier:annotationIdentifier] autorelease]; 
      if([titlestr isEqualToString:@"You are here"]) 
      { 
       customPinView.pinColor = MKPinAnnotationColorGreen; 
       NSLog(@"customPinView.pinColor = MKPinAnnotationColorGreen;"); 
      } 
      else{ 
       customPinView.pinColor = MKPinAnnotationColorPurple; 
       customPinView.selected = TRUE; 
       NSLog(@"customPinView.pinColor = MKPinAnnotationColorPurple;"); 
       UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
       [rightButton addTarget:self 
           action:@selector(ShowStoreDetail:) 
         forControlEvents:UIControlEventTouchUpInside]; 
       customPinView.rightCalloutAccessoryView = rightButton; 
      } 
      customPinView.animatesDrop = YES; 
      customPinView.canShowCallout = YES; 

      return customPinView; 
     } 
     else 
     { 
      pinView.annotation = annotation; 



      if([titlestr isEqualToString:@"You are here"]) 
      { 
       customPinView.pinColor = MKPinAnnotationColorPurple; 
       NSLog(@"customPinView.pinColor = MKPinAnnotationColorGreen;"); 
      } 
      else{ 
       customPinView.pinColor = MKPinAnnotationColorPurple; 
       customPinView.selected = TRUE; 
       NSLog(@"customPinView.pinColor = MKPinAnnotationColorPurple;"); 
       UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
       [rightButton addTarget:self 
           action:@selector(ShowStoreDetail:) 
         forControlEvents:UIControlEventTouchUpInside]; 
       customPinView.rightCalloutAccessoryView = rightButton; 
      } 


     } 
     return pinView; 

    return nil; 
}