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];

        }
    }
}

对不起我的英语不好,如果你不明白这个问题,请不要贬低,只是问,我总是在附近回答。

最好的祝福

4

1 回答 1

6

didSelectAnnotationView中,这段代码:

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

不会总是有效,因为地图视图的annotations数组无法保证注释的顺序与您添加它们的顺序相同。(有关这一点的详细信息,请参阅MKMapView 注释更改/丢失顺序?如何重新排序 MKMapView 注释数组。)

根本不能假设数组index中的注释与mapView.annotations数组中注释源数据的索引相同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 屏幕外注释图像不正确

于 2012-12-19T16:56:36.080 回答