2012-12-19 29 views
1

我的問題是,當我點擊引腳上,它給出了相同的引腳相關的錯誤信息。我認爲引腳的索引可能與數組的索引不一樣。點擊地圖針給出錯誤信息

下面是代碼:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *pinView = nil; 

    if(annotation != mapView.userLocation) 
    { 
     static NSString *defaultPinID = @"com.invasivecode.pin"; 
     pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (pinView == nil) 
      pinView = [[MKAnnotationView alloc] 
         initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 
     pinView.canShowCallout = YES; 

     if ((annotation.coordinate.latitude == mapLatitude) && (annotation.coordinate.longitude == mapLongitude)) { 

       if ([estadoUser isEqualToString:@"online"]) 
        { 
         // NSLog(@"ONLINE"); 
         pinView.image = [UIImage imageNamed:@"1352472516_speech_bubble_green.png"]; //as suggested by Squatch 
        }else{ 
         //NSLog(@"OFFLINE"); 
         pinView.image = [UIImage imageNamed:@"1352472468_speech_bubble_red.png"]; 
        } 
     } 
     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
     [rightButton addTarget:self 
         action:@selector(showDetails) 
       forControlEvents:UIControlEventTouchUpInside]; 
     pinView.rightCalloutAccessoryView = rightButton; 

    } else { 
     [mapView.userLocation setTitle:@"I am here"]; 
     } 
    return pinView; 
} 

-(void)showDetails 
{ 
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; 
    DMChatRoomViewController *_controller = [storyboard instantiateViewControllerWithIdentifier:@"DmChat"]; 
    [self presentViewController:_controller animated:YES completion:nil]; 
} 

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    if ([view.annotation isKindOfClass:[DisplayMap class]]) 
    { 
     NSInteger index = [mapView.annotations indexOfObject:view.annotation]  

     DisplayMap *annotation = (DisplayMap *)view.annotation; 

     NSMutableDictionary *item = [allMapUsers objectAtIndex:index]; 

    //HERE DOES NOT DISPLAY THE INFO ON THE CORRECT PLACE 

     NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults]; 
     [standardUserDefaults setObject:[[allMapUsers objectAtIndex:index] objectId] forKey:@"userSelecionadoParaChat"]; 


     [standardUserDefaults synchronize]; 
    } 
} 

-(void)reloadMap 
{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    for (int i=0; i<allMapUsers.count; i++) 
    { 
     NSMutableDictionary *item = [allMapUsers objectAtIndex:i]; 

     NSLog(@"index=%i para objectID=%@",i,[[allMapUsers objectAtIndex:i] objectId]); 

     if (([[item valueForKey:@"estado"] isEqualToString:@"offline"] && [[defaults stringForKey:@"showOfflineUsers"] isEqualToString:@"no"]) || [[item valueForKey:@"estado"] isEqualToString:@""]) { 

     }else{ 

     estadoUser = [item valueForKey:@"estado"]; 

     [outletMapView setMapType:MKMapTypeStandard]; 
     [outletMapView setZoomEnabled:YES]; 
     [outletMapView setScrollEnabled:YES]; 

     MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
     region.center.latitude = [[item valueForKey:@"Latitude"] floatValue]; 

     region.center.longitude = [[item valueForKey:@"Longitude"] floatValue]; 

     region.span.longitudeDelta = 81; 
     region.span.latitudeDelta = 80; 
     [outletMapView setRegion:region animated:YES]; 
     ///// 
     mapLatitude = [[item valueForKey:@"Latitude"] floatValue]; 
     mapLongitude = [[item valueForKey:@"Longitude"] floatValue]; 

     CLLocationCoordinate2D locationco = {mapLatitude,mapLongitude}; 

     ann = [[DisplayMap alloc] init]; 
     ann.coordinate = locationco; 


     ann.title = [item valueForKey:@"username1"]; 
      NSLog(@"ann.title=%@ para objectID=%@",[item valueForKey:@"username1"],[[allMapUsers objectAtIndex:i] objectId]); 
     ann.subtitle = [item valueForKey:@"estado"]; 
     ann.coordinate = region.center; 
     [outletMapView addAnnotation:ann]; 

     } 
    } 
} 

對不起我的英文不好,如果你不明白的問題,請不要downrate,就問我,我總是繞來回答。

問候

回答

6

didSelectAnnotationView,該代碼:

NSInteger index = [mapView.annotations indexOfObject:view.annotation]  
DisplayMap *annotation = (DisplayMap *)view.annotation; 
NSMutableDictionary *item = [allMapUsers objectAtIndex:index]; 

並不總是可行的,因爲地圖視圖的annotations陣列是沒有辦法保證有標註在同一爲了你添加它們。 (關於這一點的詳細信息,請參閱MKMapView annotations changing/losing order?How to reorder MKMapView annotations array。)

不能在所有mapView.annotations陣列中的註釋的index是註釋的源數據在你的allMapUsers陣列相同的索引承擔。


您可以做的是在註釋本身中保留對源對象的引用。

例如,NSMutableDictionary屬性添加到您的DisplayMap類:

@property (nonatomic, retain) NSMutableDictionary *sourceDictionary; 

在創建註釋,設置屬性:

ann = [[DisplayMap alloc] init]; 
ann.sourceDictionary = item; // <-- keep ref to source item 
ann.coordinate = locationco; 

然後在didSelectAnnotationView

DisplayMap *annotation = (DisplayMap *)view.annotation; 
NSMutableDictionary *item = annotation.sourceDictionary; 


另一個可能的問題是在這裏viewForAnnotation

pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotation... 
if (pinView == nil) 
    pinView = [[MKAnnotationView alloc] ... 
pinView.canShowCallout = YES; 

如果離隊返回先前使用的視圖,它是annotation屬性依然會指向它以前用於註釋。當使用取出的觀點,你必須更新其annotation屬性設置爲當前註釋:

pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotation... 
if (pinView == nil) 
    pinView = [[MKAnnotationView alloc] ... 
else 
    pinView.annotation = annotation; // <-- add this 
pinView.canShowCallout = YES; 

詳情請參閱MKMapView Off Screen Annotation Image Incorrect

+0

這就是爲什麼你在24.8k。 V.通過回答。 – Craig

+0

謝謝你幫了我很多。 – MonkeyBlue