MKAnnotation 的自定义图像

发布于 2024-12-05 07:53:29 字数 427 浏览 1 评论 0原文

我创建了一个注释,并将其添加到 MKMapView 中。我该如何使用自定义图像而不是标准的红色图钉?

@interface AddressAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    MKPinAnnotationColor pinColor;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic, assign) MKPinAnnotationColor pinColor;
@end

I have created an annotation which I'm adding to an MKMapView. How would I go about having a custom image instead of the standard red pin?

@interface AddressAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    MKPinAnnotationColor pinColor;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic, assign) MKPinAnnotationColor pinColor;
@end

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

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

发布评论

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

评论(3

躲猫猫 2024-12-12 07:53:29

MKMapView 从其委托方法获取其 pin 视图 mapView:viewForAnnotation: 所以你必须:

  1. 设置你的视图控制器作为地图的代表。
  2. 在控制器中实现 mapView:viewForAnnotation: 。

将您的控制器设置为委托

@interface MapViewController : UIViewController <MKMapViewDelegate>

使用委托协议标记接口。这让您可以在 Interface Builder (IB) 中将控制器设置为 MKMapView 的委托。打开包含地图的 .xib 文件,右键单击 MKMapView,然后将 delegate 出口拖动到控制器。
如果您更喜欢使用代码而不是 IB,请在控制器的 viewDidLoad 方法中添加 self.yourMapView.delegate=self; 。结果是一样的。

实现mapView:viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // this part is boilerplate code used to create or reuse a pin annotation
    static NSString *viewId = @"MKPinAnnotationView";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView*) 
        [self.mapView dequeueReusableAnnotationViewWithIdentifier:viewId];
    if (annotationView == nil) {
        annotationView = [[[MKPinAnnotationView alloc] 
            initWithAnnotation:annotation reuseIdentifier:viewId] autorelease];
    }
    // set your custom image
    annotationView.image = [UIImage imageNamed:@"emoji-ghost.png"];
    return annotationView;
}

MKMapView gets its pin views from its delegate method mapView:viewForAnnotation: So you have to:

  1. Set your view controller as the map's delegate.
  2. Implement mapView:viewForAnnotation: in your controller.

Set your controller as delegate

@interface MapViewController : UIViewController <MKMapViewDelegate>

Mark the interface with the delegate protocol. This let's you set the controller as MKMapView's delegate in Interface Builder (IB). Open the .xib file containing your map, right click the MKMapView, and drag the delegate outlet to your controller.
If you prefer to use code instead IB, add self.yourMapView.delegate=self; in the controller's viewDidLoad method. The result will be the same.

Implement mapView:viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // this part is boilerplate code used to create or reuse a pin annotation
    static NSString *viewId = @"MKPinAnnotationView";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView*) 
        [self.mapView dequeueReusableAnnotationViewWithIdentifier:viewId];
    if (annotationView == nil) {
        annotationView = [[[MKPinAnnotationView alloc] 
            initWithAnnotation:annotation reuseIdentifier:viewId] autorelease];
    }
    // set your custom image
    annotationView.image = [UIImage imageNamed:@"emoji-ghost.png"];
    return annotationView;
}
乖不如嘢 2024-12-12 07:53:29

重写 mapView:viewForAnnotation: 委托方法。如果 annotation 参数指向您的自定义注释之一,则返回一个符合您喜好的自定义视图。

Override the mapView:viewForAnnotation: delegate method. If the annotation param points to one of your custom annotations, return a custom view that looks to your liking.

何时共饮酒 2024-12-12 07:53:29

要设置自定义图像而不是标准 MKPinAnnotationView ,唯一的方法是将 MKAnnotationView 与函数 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation 结合使用:(id)注释。示例如下:

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

     if ([annotation isKindOfClass:[MKUserLocation class]]) {
                return  nil;
     }

     static NSString *identifier = @"Annotation";

     MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

     if (!aView) {
          aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
          aView.image = [UIImage imageNamed:@"Untitled1.png"];
          aView.canShowCallout = YES;
          aView.draggable = YES;
     } else {
          aView.annotation = annotation;
     }

     return pin;
}

对于 aView.image 值,您可以设置自己的图像。还要查看 MKAnnotationView类引用可以更好地处理它。

To set custom image instead of standart MKPinAnnotationView the only way to do that is to use MKAnnotationView with function - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation. Here's the example:

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

     if ([annotation isKindOfClass:[MKUserLocation class]]) {
                return  nil;
     }

     static NSString *identifier = @"Annotation";

     MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

     if (!aView) {
          aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
          aView.image = [UIImage imageNamed:@"Untitled1.png"];
          aView.canShowCallout = YES;
          aView.draggable = YES;
     } else {
          aView.annotation = annotation;
     }

     return pin;
}

For the aView.image value You can set Your own image. And also look into MKAnnotationView class reference to handle better with it.

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