Getting the title to show up when map loads with m

2020-04-19 07:18发布

I am able to get a map to show up and a pin to drop where I want in my iphone app project, but I want the title and subtitle to appear when the view loads. Here is the code I'm using. I thought putting in [mapView selectAnnotation:annotation animated:YES];

would work, but it doesn't. Does anyone know how to do this?

Thanks

CLLocationCoordinate2D coord = {latitude: 32.02008, longitude: -108.479707};

    [self.view addSubview:mapView];


MapController *annotation = [[MapController alloc]  initWithCoordinate:coord];
annotation.currentPoint = [NSNumber numberWithInt:1];
annotation.mTitle = @"MyTitle";
annotation.mSubTitle = @"My Address";
[mapView selectAnnotation:annotation animated:YES];
[mapView addAnnotation:annotation];
[annotation release];

1条回答
我想做一个坏孩纸
2楼-- · 2020-04-19 07:37

Calling selectAnnotation before it's added to the map won't work and even putting it after the addAnnotation line will not work because the annotation view hasn't been drawn on the map yet.

You'll need to use the didAddAnnotationViews delegate method which is called when annotations are ready to manipulate:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    id<MKAnnotation> myAnnotation = [mapView.annotations objectAtIndex:0];
    [mapView selectAnnotation:myAnnotation animated:YES];
}

The example just assumes you have one annotation and gets it from the mapView's annotations array. You could also hold a reference to your annotation with an ivar.

Make sure you have set the mapView's delegate property otherwise the method won't be called.

查看更多
登录 后发表回答