当地图加载mkmap时获取标题显示 [英] Getting the title to show up when map loads with mkmap

查看:181
本文介绍了当地图加载mkmap时获取标题显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以得到一个地图显示和一个别针放在我想在我的iphone应用程序项目,但我想要的标题和字幕出现时视图加载。这里是我使用的代码。我想在
[mapView selectAnnotation:annotation animated:YES];



将工作,但它不工作。



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 = @我的地址;
[mapView selectAnnotation:annotation animated:YES];
[mapView addAnnotation:annotation];
[annotation release];


解决方案

在添加到地图之前调用selectAnnotation工作,甚至将其放在addAnnotation行后面,因为注释视图还没有在地图上绘制。



您需要使用didAddAnnotationViews委托方法,当注释准备好处理时被调用:

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

示例假设您有一个注释,并从mapView的注释数组。



请确保您设置了mapView的委托属性,否则方法将不会被调用。


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

解决方案

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.

这篇关于当地图加载mkmap时获取标题显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆