Getting the title to show up when map loads with mkmap

若如初见. 提交于 2019-12-02 08:23:55

问题


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:


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.



来源:https://stackoverflow.com/questions/4151094/getting-the-title-to-show-up-when-map-loads-with-mkmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!