0

我正在設計一個地圖視圖,的針腳。我還在每個引腳上添加了一個自定義視圖。現在問題是,當我的地圖視圖添加所有的引腳,然後在地圖上它不顯示我的最後一個引腳。而不是最後一針,它只會顯示自定義視圖,當我點擊它時,它不會隱藏。並且在某些針腳上不添加自定義視圖。我顯示我下面的代碼... class.h爲什麼它會在我的地圖視圖中顯示最後一個引腳的調用註釋類型

@interface MappingBusinesses:{的UIViewController

MKMapView *_mapView; 
CalloutMapAnnotation *_calloutAnnotation; 
MKAnnotationView *_selectedAnnotationView; 
Places *Place_Object; 
DisplayMap *dm; 
NSMutableArray *routepoints; 
CSMapRouteLayerView *routeView; 
UICRouteOverlayMapView *routeOverlayView; 
UICGDirections *diretions; 
Yelp_OnTheWayAppDelegate *appDelegate; 
NSArray *wayPoints; 
UICGTravelModes travelMode; 
DetailView *DV_Object; 
UIActivityIndicatorView *aiv; 
UILabel *label; 
UIView *prosView; 
BusinessData *bd_object; 

}

class.m

- (void)viewDidLoad { 
[super viewDidLoad]; 

appDelegate = (Yelp_OnTheWayAppDelegate *)[[UIApplication sharedApplication] delegate]; 
[email protected]"Map"; 
//[NSThread detachNewThreadSelector:@selector(start) toTarget:self withObject:nil]; 
label.text=[NSString stringWithFormat:@"%d places along your route",[appDelegate.busines_Aray count]]; 
[self.mapView setMapType:MKMapTypeStandard]; 
[self.mapView setZoomEnabled:YES]; 
[self.mapView setScrollEnabled:YES]; 
[self.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; 
[self.mapView setRegion:region animated:YES]; 

dm=[[DisplayMap alloc]init]; 
for(int i=0;i<[appDelegate.mapPin_aray count];i++) 
{ 
    CLLocationCoordinate2D coord; 
    coord.latitude=[[appDelegate.mapPin_lat objectAtIndex:i] floatValue]; 
    coord.longitude=[[appDelegate.mapPin_long objectAtIndex:i] floatValue]; 
    self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:coord.latitude andLongitude:coord.longitude] ; 
    [self.mapView addAnnotation:self.calloutAnnotation]; 
    NSLog(@"coordinate of number%i is %f and %f",i,coord.latitude,coord.longitude); 

    } 

}

- (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 *)[self.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"; 
if (annotation == self.calloutAnnotation) { 
    CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier]; 
    if (!calloutMapAnnotationView) { 
     calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation 
                     reuseIdentifier:@"CalloutAnnotation"] autorelease]; 
     calloutMapAnnotationView.contentHeight = 70.0f; 
     bd_object=[appDelegate.busines_Aray objectAtIndex:self.calloutAnnotation.pinID]; 
     UITextField *txtname=[[UITextField alloc] initWithFrame:CGRectMake(0, 5, 150, 30)]; 
     txtname.text=[NSString stringWithFormat:@"%@",bd_object.name_business ]; 
     txtname.userInteractionEnabled=NO; 
     txtname.font = [UIFont boldSystemFontOfSize:18]; 
     txtname.font=[UIFont fontWithName:@"Arial" size:18]; 
     txtname.textColor = [UIColor whiteColor]; 
     txtname.opaque = false; 
     txtname.backgroundColor = [UIColor clearColor]; 
     [calloutMapAnnotationView.contentView addSubview:txtname]; 
     [txtname release]; 

     UILabel *gpsCoordinatesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 70, 20)]; 
     gpsCoordinatesLabel.text = @"Categories:"; 
     gpsCoordinatesLabel.font = [UIFont boldSystemFontOfSize:14]; 
     gpsCoordinatesLabel.font=[UIFont fontWithName:@"Arial" size:14]; 
     gpsCoordinatesLabel.textColor = [UIColor whiteColor]; 
     gpsCoordinatesLabel.opaque = false; 
     gpsCoordinatesLabel.backgroundColor = [UIColor clearColor]; 
     gpsCoordinatesLabel.textAlignment = UITextAlignmentRight; 
     [calloutMapAnnotationView.contentView addSubview:gpsCoordinatesLabel]; 
     [gpsCoordinatesLabel release]; 

     UITextField *txtcate=[[UITextField alloc] initWithFrame:CGRectMake(70, 40, 100, 30)]; 
     txtcate.text=[NSString stringWithFormat:@"%@", bd_object.cat_business]; 
     txtcate.userInteractionEnabled=NO; 
     txtcate.font = [UIFont boldSystemFontOfSize:18]; 
     txtcate.font=[UIFont fontWithName:@"Arial" size:18]; 
     txtcate.textColor = [UIColor whiteColor]; 
     txtcate.opaque = false; 
     txtcate.backgroundColor = [UIColor clearColor]; 
     [calloutMapAnnotationView.contentView addSubview:txtcate]; 
     [txtcate release]; 

     UITextField *txtReview=[[UITextField alloc] initWithFrame:CGRectMake(190, 40, 40, 30)]; 
     txtReview.text=[NSString stringWithFormat:@"%d", bd_object.noofreview]; 
     txtReview.userInteractionEnabled=NO; 
     txtReview.font = [UIFont boldSystemFontOfSize:18]; 
     txtReview.textColor = [UIColor whiteColor]; 
     txtReview.opaque = false; 
     txtReview.backgroundColor = [UIColor clearColor]; 
     [calloutMapAnnotationView.contentView addSubview:txtReview]; 
     [txtReview release]; 

     UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(210, 40, 31, 23)]; 
     [imgView setImage:[UIImage imageNamed:@"reviewwhite.png"]]; 
     [calloutMapAnnotationView.contentView addSubview:imgView]; 
     [imgView release]; 


    } 
    calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView; 
    calloutMapAnnotationView.mapView = self.mapView; 

    return calloutMapAnnotationView; 
} 

else { 
    MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                      reuseIdentifier:@"CustomAnnotation"] autorelease]; 
    annotationView.canShowCallout = NO; 
    annotationView.pinColor = MKPinAnnotationColorRed; 
    return annotationView; 
} 

MKAnnotationView *av; 
return av; 

}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { 
if (self.calloutAnnotation != nil) { 
    self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude]; 
} else { 
    self.calloutAnnotation.latitude = view.annotation.coordinate.latitude; 
    self.calloutAnnotation.longitude = view.annotation.coordinate.longitude; 
} 

[self.mapView addAnnotation:self.calloutAnnotation]; 
self.selectedAnnotationView = view; 

}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view { 
if (self.calloutAnnotation != nil) { 
    [self.mapView removeAnnotation: self.calloutAnnotation]; 
} 

}

回答

0

不知道,但我認爲這個問題可能是你在viewDidLoad中最初的循環中,你是在回憶中的自我註解。 calloutAnnotation,然後在didSelect和didDeselect中檢查它。

問題是每次循環都會替換self.calloutAnnotation,所以最後它只會引用最後添加的註釋。例如,當用戶選擇不同的註釋時,您可以更改self.calloutAnnotation的lat/long並再次添加它,但這會更改數組中添加的最後一個註釋的緯度/經度。

+0

感謝您的支持。 – iRavan12

相關問題