使用 mkmap 加载地图时显示标题

发布于 2024-10-01 12:15:35 字数 599 浏览 1 评论 0原文

我能够在 iphone 应用程序项目中显示地图并在所需位置放置图钉,但我希望在视图加载时显示标题和副标题。这是我正在使用的代码。我以为放入 [mapView selectAnnotation:注释动画:是];

会起作用,但事实并非如此。有谁知道该怎么做?

谢谢

CLLocationCoordinate2D coord = {纬度:32.02008,经度:-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];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕烟庭风 2024-10-08 12:15:35

在将其添加到地图之前调用 selectAnnotation 将不起作用,甚至将其放在 addAnnotation 行之后也不起作用,因为注释视图尚未绘制在地图上。

您需要使用 didAddAnnotationViews 委托方法,该方法在注释准备好操作时调用:

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

该示例仅假设您有一个注释并从 mapView 的注释数组中获取它。您还可以使用 ivar 保存对注释的引用。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文