显示地图注释内的图像

发布于 2024-10-16 11:26:23 字数 326 浏览 0 评论 0原文

使用下面的代码,我可以显示标题和副标题,并且我想在其中显示图像。这可能吗?如果是这样,请帮帮我。

MKPointAnnotation *aAnnotationPoint = [[MKPointAnnotation alloc] init];

aAnnotationPoint.title = @"Virginia";

aAnnotationPoint.subtitle = @"Test of sub title";

// Add the annotationPoint to the map
[myMapView addAnnotation:aAnnotationPoint];

Using the code below, I'm able to show the title and subtitle, and I want to show an image inside it. Is that possible? If so, please help me out.

MKPointAnnotation *aAnnotationPoint = [[MKPointAnnotation alloc] init];

aAnnotationPoint.title = @"Virginia";

aAnnotationPoint.subtitle = @"Test of sub title";

// Add the annotationPoint to the map
[myMapView addAnnotation:aAnnotationPoint];

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

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

发布评论

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

评论(1

摇划花蜜的午后 2024-10-23 11:26:23

我假设您在注释的标注中显示标题和副标题(如果您使用的是图钉,则点击图钉时会出现灰色框)。如果是这样,标注有两个视图供您自定义(leftCalloutAccessoryView 和 rightCalloutAccessoryView(两者均在注释视图上设置)),

因此,如果您希望在点击图钉时图像出现在图钉上方的灰色框中,您可以执行以下操作因此,通过实现这样的委托方法来自定义注释视图:

-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
  // If you are showing the users location on the map you don't want to change it
  MKAnnotationView *view = nil;
  if (annotation != mapView.userLocation) {
    // This is not the users location indicator (the blue dot)
    view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
    if (!view) {
      // Could not reuse a view ...

      // Creating a new annotation view, in this case it still looks like a pin
      view = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease];
      view.canShowCallOut = YES; // So that the callout can appear

      UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someName"]];
      myImageView.frame = CGRectMake(0,0,31,31); // Change the size of the image to fit the callout

      // Change this to rightCallout... to move the image to the right side
      view.leftCalloutAccessoryView = myImageView;
      [myImageView release], myImageView = nil;
    }
  }
  return view;
}

但是,如果您想要的只是直接在地图上(而不是在标注中)显示大量图像,那么您可以使用相同的委托方法,设置注释视图的“image”属性,如下所示:

-(MKAnnotationView*)mapView:(MKMapView)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
      // If you are showing the users location on the map you don't want to change it
      MKAnnotationView *view = nil;
      if (annotation != mapView.userLocation) {
        // This is not the users location indicator (the blue dot)
        view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
        if (!view) {
          // Could not reuse a view ...

          // Creating a new annotation view
          view = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease];

          // This will rescale the annotation view to fit the image
          view.image = [UIImage imageNamed:@"someName"];
        }
      }
     return view;
}

我希望能回答您的问题

I'm assuming that you are showing the title and subtitle in the callout for the annotation (the gray box that appears when you tap on the pin, if you are using a pin). If so, the callout has two views for you to customize (leftCalloutAccessoryView and rightCalloutAccessoryView (both are set on the annotation view))

SO if you want the image to appear in the gray box above your pin when you tap on the pin you can do so by customizing the annotation view by implementing a delegate method like this:

-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
  // If you are showing the users location on the map you don't want to change it
  MKAnnotationView *view = nil;
  if (annotation != mapView.userLocation) {
    // This is not the users location indicator (the blue dot)
    view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
    if (!view) {
      // Could not reuse a view ...

      // Creating a new annotation view, in this case it still looks like a pin
      view = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease];
      view.canShowCallOut = YES; // So that the callout can appear

      UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someName"]];
      myImageView.frame = CGRectMake(0,0,31,31); // Change the size of the image to fit the callout

      // Change this to rightCallout... to move the image to the right side
      view.leftCalloutAccessoryView = myImageView;
      [myImageView release], myImageView = nil;
    }
  }
  return view;
}

However, if all you wanted was to show a lot of images directly on the map (not in the callouts) then you could, using the same delegate method, set the "image" property of the annotation view, like this:

-(MKAnnotationView*)mapView:(MKMapView)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
      // If you are showing the users location on the map you don't want to change it
      MKAnnotationView *view = nil;
      if (annotation != mapView.userLocation) {
        // This is not the users location indicator (the blue dot)
        view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
        if (!view) {
          // Could not reuse a view ...

          // Creating a new annotation view
          view = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease];

          // This will rescale the annotation view to fit the image
          view.image = [UIImage imageNamed:@"someName"];
        }
      }
     return view;
}

I hope that answers your question

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