在地图注释中显示图像

在地图注释中显示图像

本文介绍了在地图注释中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我可以显示标题和副标题,我想在其中显示图像。那可能吗?如果有,请帮助我。

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


推荐答案

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

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

我希望能回答你的问题

这篇关于在地图注释中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:17