【问题标题】:How to show Image over Map not as Pin but as seperate image? [duplicate]如何在地图上显示图像而不是作为图钉而是作为单独的图像? [复制]
【发布时间】:2013-07-29 11:26:26
【问题描述】:

我想在地图上显示两个标志,但它们不是图钉。我为此搜索了很多,但找不到解决方案,只能添加为 pin。

请帮忙

【问题讨论】:

    标签: iphone ios ipad uiimage mapkit


    【解决方案1】:

    您正在寻找地图叠加层MKOverlayView

    查看这些教程:

    创建叠加层

    • 创建MKOverlayView

    创建MKOverlayView 的子类,例如:

    .h #进口 #导入

    @interface MapOverlayView : MKOverlayView
    {
    }
    @end
    

    .m

    #import "MapOverlayView.h"
    
    @implementation MapOverlayView
    
    - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)ctx
    {
    
        UIImage *image = [UIImage imageNamed:@"yourImage.png"];
        CGImageRef imageReference = image.CGImage;
    
        MKMapRect theMapRect = [self.overlay boundingMapRect];
        CGRect theRect = [self rectForMapRect:theMapRect];
    
        CGContextDrawImage(ctx, theRect, imageReference); 
    }
    
    @end
    
    • 添加叠加层

    实现viewForOverlay:,在其中创建叠加层并添加到地图。

    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
    {
    
        MapOverlay *mapOverlay = (MapOverlay *)overlay;    
        MapOverlayView *mapOverlayView = [[[MapOverlayView alloc] initWithOverlay:mapOverlay] autorelease];
    
        return mapOverlayView;
    }
    

    【讨论】:

    • 我认为 MKAnnotationView 更适合用例
    【解决方案2】:

    您可以使用MKAnnotationViewimage 属性来做到这一点,就像viewForAnnotation: 方法中的波纹管一样..

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    
        static NSString *identifier = @"Current";           
        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    
        if (annotationView == nil)
        {
            annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
        }
    
        if (annotation == mapView.userLocation)
            return nil;
    
        annotationView.image = [UIImage imageNamed:@"yourImageName.png"];
        annotationView.annotation = annotation;            
        annotationView.canShowCallout = YES;
        return annotationView;
    }
    

    【讨论】:

    • 在哪里设置图片位置?
    • 当你在你的类中添加任何带有 lat-long 的位置时,这个委托方法会自动调用,并且这个图像会随着那个位置的 lat-log 下降..
    • @NG_SE 从这个链接iphoneapp-dev.blogspot.in/2011/01/…查看 mkmapview 上自定义引脚的演示示例@
    【解决方案3】:

    试试这个

    annotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"try"];
        annotation.canShowCallout = YES;
    
        annotation.image = [UIImage imageNamed:@"image.png"];
    
    
        return annotation;
    

    并且不要使用 annotation.animatesDrop 属性。

    【讨论】:

      【解决方案4】:

      如果你在谈论MKMapView:
      要显示图像而不是 Pin 注释,您需要覆盖 MKMapView 的方法

      - (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation
      

      像这样:

      - (MKAnnotationView *)viewForAnnotation:(id < MKAnnotation >)annotation{
          static NSString* annotationIdentifier = @"Identifier";
          MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
          if(annotationView)
              return annotationView;
          else
          {
              MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
              annotationView.canShowCallout = YES;
      // here you need to give the image you want instead of pin
                  annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"balloon.png"]];        
                  return annotationView;
              }
              return nil;
              }
      

      【讨论】:

      猜你喜欢
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      相关资源
      最近更新 更多