Alex Cher
Alex Cher

Reputation: 11

MKMapView addAnnotation not on the MapView

I need some help because i can't to display annotation in my mapView. I'm trying to update the annotations with this function :

- (void)addAnnotationsInMap:(NSMutableDictionary *)dico
{
    NSLog(@"Agences Dictionary dico4324 : %@", dico);  
    if ([dico count] > 0)
    {
        NSLog(@"Count : %d", [dico count]);
        for(int i = 0; i < [dico count]; i++)
        {
            CGFloat latDelta = [[[dico objectForKey:[NSString stringWithFormat:@"%d", i]] objectForKey:@"latitude"] floatValue];
            CGFloat longDelta   = [[[dico objectForKey:[NSString stringWithFormat:@"%d", i]] objectForKey:@"longitude"] floatValue];      
            CLLocationCoordinate2D newCoord =
            {
                (latDelta),
                (longDelta)
            };      
            MapPointAnnotations *mp = [[MapPointAnnotations alloc]
                                       initWithCoordinate:newCoord
                                                    title:[[dico objectForKey:[NSString stringWithFormat:@"%d", i]] objectForKey:@"name"]
                                                 subTitle:[[dico objectForKey:[NSString stringWithFormat:@"%d", i]] objectForKey:@"street"]];  
            [agenceMapView addAnnotation:mp];    
        }
    }
}

If i call my function in the same class (LocalizeViewController) with [self addAnnotationsInMap:Dico], the annotations are on the mapView (agenceMapView).

I need to call my function from an other class (RightSideBarViewController), but the annotations not display on the mapView if i call on the RightSideBarViewController class.

Where i call the function addAnnotationsInMap (Class RightSideBarViewController) :

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"LocaliserTop";
    UIStoryboard *storyboard;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];
    }
    LocalizeViewController *localizeViewController = (LocalizeViewController *)[storyboard instantiateViewControllerWithIdentifier:identifier];    
    [self.slidingViewController anchorTopViewOffScreenTo:ECLeft animations:nil onComplete:^{
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        self.slidingViewController.topViewController = localizeViewController;
        self.slidingViewController.topViewController.view.frame = frame;
        [self.slidingViewController resetTopView];
    }];
    [localizeViewController addAnnotationsInMap:agencesDictionary];
}

Do you have some ideas ?

Upvotes: 1

Views: 886

Answers (1)

sch
sch

Reputation: 27536

You should not call

[localizeViewController addAnnotationsInMap:agencesDictionary];

right after

LocalizeViewController *localizeViewController = (LocalizeViewController *)[storyboard instantiateViewControllerWithIdentifier:identifier];

because agenceMapView is still nil. So you are basically doing

[nil addAnnotation:mp];

To correct that, you should wait until the localizeViewController loads from the nib file (which take some time).

For example, you can put

localizeViewController.annotations = agencesDictionary;

instead of

[localizeViewController addAnnotationsInMap:agencesDictionary];

Then in viewDidLoad

[self addAnnotationsInMap:annotations];

Upvotes: 1

Related Questions