开发者

Getting the title to show up when map loads with mkmap

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:anno开发者_Python百科tation animated:YES];
[mapView addAnnotation:annotation];
[annotation release];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜