如何创建图像叠加层并将其添加到 MKMapView?

发布于 2024-10-21 07:27:22 字数 162 浏览 1 评论 0 原文

我正在尝试学习 MapKit,现在正在尝试将图像作为叠加层添加到地图视图中。 我似乎找不到任何示例代码来帮助解释如何执行此操作。

你们能帮助我如何创建 MKOverlay 并将它们添加到 MKMapKit 中吗?

提前致谢..

I am trying to learn MapKit and am now trying to add an image as an overlay to the map view.
I can't seem to find any sample code to help explain how to do this.

Can you guys please help me how to create MKOverlays and add them to MKMapKit.

Thanks in advance..

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

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

发布评论

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

评论(4

养猫人 2024-10-28 07:27:22

以下是如何将 UIImage 设置为 MKMapView 叠加层的示例。一些参数(坐标和图像路径)是固定的,但我猜代码可以很容易地改变。

创建一个符合 MKOverlay 的类:

MapOverlay.h

@interface MapOverlay : NSObject <MKOverlay> {

}

- (MKMapRect)boundingMapRect;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@end

MapOverlay.m

@implementation MapOverlay

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
    self = [super init];
    if (self != nil) {


    }
    return self;
}

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D coord1 = {
        37.434999,-122.16121
    };

    return coord1;
}

- (MKMapRect)boundingMapRect
{

    MKMapPoint upperLeft = MKMapPointForCoordinate(self.coordinate);

    MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, 2000, 2000);
    return bounds;
}


@end

创建一个符合 MKOverlayView 的类:

MapOverlayView.h

@interface MapOverlayView : MKOverlayView {

}

@end

MapOverlayView.m

@implementation MapOverlayView

- (void)drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)ctx
{

    UIImage *image          = [[UIImage imageNamed:@"image.png"] retain];

    CGImageRef imageReference = image.CGImage;

    MKMapRect theMapRect    = [self.overlay boundingMapRect];
    CGRect theRect           = [self rectForMapRect:theMapRect];
    CGRect clipRect     = [self rectForMapRect:mapRect];

    CGContextAddRect(ctx, clipRect);
    CGContextClip(ctx);

    CGContextDrawImage(ctx, theRect, imageReference);

    [image release]; 

}


@end

将叠加层添加到 MKMapView:

MapOverlay * mapOverlay = [[MapOverlay alloc] init];
[mapView addOverlay:mapOverlay];
[mapOverlay release];

在 mapView 委托上,实现 viewForOverlay :

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {

    MapOverlay *mapOverlay = overlay;
    MapOverlayView *mapOverlayView = [[[MapOverlayView alloc] initWithOverlay:mapOverlay] autorelease];

    return mapOverlayView;

}

希望有帮助!

Here is an example to how to sets a UIImage to a MKMapView overlay. Some parameter (coordinates and image path) are fixed, but the code can be easily changed, I guess.

Create an class that conforms to MKOverlay:

MapOverlay.h

@interface MapOverlay : NSObject <MKOverlay> {

}

- (MKMapRect)boundingMapRect;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@end

MapOverlay.m

@implementation MapOverlay

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
    self = [super init];
    if (self != nil) {


    }
    return self;
}

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D coord1 = {
        37.434999,-122.16121
    };

    return coord1;
}

- (MKMapRect)boundingMapRect
{

    MKMapPoint upperLeft = MKMapPointForCoordinate(self.coordinate);

    MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, 2000, 2000);
    return bounds;
}


@end

Create an class that conforms to MKOverlayView:

MapOverlayView.h

@interface MapOverlayView : MKOverlayView {

}

@end

MapOverlayView.m

@implementation MapOverlayView

- (void)drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)ctx
{

    UIImage *image          = [[UIImage imageNamed:@"image.png"] retain];

    CGImageRef imageReference = image.CGImage;

    MKMapRect theMapRect    = [self.overlay boundingMapRect];
    CGRect theRect           = [self rectForMapRect:theMapRect];
    CGRect clipRect     = [self rectForMapRect:mapRect];

    CGContextAddRect(ctx, clipRect);
    CGContextClip(ctx);

    CGContextDrawImage(ctx, theRect, imageReference);

    [image release]; 

}


@end

Add the overlay to the MKMapView:

MapOverlay * mapOverlay = [[MapOverlay alloc] init];
[mapView addOverlay:mapOverlay];
[mapOverlay release];

On the mapView delegate, implement the viewForOverlay:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {

    MapOverlay *mapOverlay = overlay;
    MapOverlayView *mapOverlayView = [[[MapOverlayView alloc] initWithOverlay:mapOverlay] autorelease];

    return mapOverlayView;

}

Hope it helps!

浮世清欢 2024-10-28 07:27:22

文档:

示例代码:

Documentation:

Sample code:

不顾 2024-10-28 07:27:22

在寻找问题的解决方案时,我不断回到这个问题,这里 还有这里

他们都是回答我需要回答的问题,但相反。

我需要确定 UIView(即 MapView 上的绘制方块)是否包含地图图钉,如果它确实让我知道,以便我可以删除它或选择它等。

最后,在尝试将我的 UIView 转换为某些 MKCooperativeRegion 或类似内容之后,我思想工作倒退。我没有将 UIView 或正方形变成疯狂的地图,而是将图钉变成简单的 CGPoints 并做了一个简单的 Rect contains point。这最终成为我的解决方案..大约 2 小时后!

希望这可以帮助以后的人

 func findMapPinsWithinBox() {

    guard let myBox = myBox else { return } // some UIView you have drawn or placed on map

    let visibleMapRect = mapView.visibleMapRect
    let visibleAnnotations =  mapView.annotationsInMapRect(visibleMapRect)

    for pin in visibleAnnotations {

        // depending on design you may need to do some casting here
        // ie let pin = pin as! MyMapAnnotation

        let pinsCGPoint = mapView.convertCoordinate(pin.coordinate, toPointToView: self.view)

        if myBox.frame.contains(pinsCGPoint) {
            print("Pin Found within the Box")
            // select pin or delete etc
        }
    }
}

In the search for a solution for my problem, I keep coming back to this question, and here and also here

They were all answering the question I needed answered but in reverse.

I needed to determine if a UIView, i.e. a draw square on my MapView contains a map pin, if it does let me know so I can remove it or select it etc.

Finally after trying to convert my UIView into some MKCoordinateRegion or alike, I thought work backwards. Instead of turning my UIView or square into map madness, I turned the pins into simple CGPoints and did a simple Rect contains point. Which ended up being my solution.. after about 2 hrs!

Hope this help someone down the line

 func findMapPinsWithinBox() {

    guard let myBox = myBox else { return } // some UIView you have drawn or placed on map

    let visibleMapRect = mapView.visibleMapRect
    let visibleAnnotations =  mapView.annotationsInMapRect(visibleMapRect)

    for pin in visibleAnnotations {

        // depending on design you may need to do some casting here
        // ie let pin = pin as! MyMapAnnotation

        let pinsCGPoint = mapView.convertCoordinate(pin.coordinate, toPointToView: self.view)

        if myBox.frame.contains(pinsCGPoint) {
            print("Pin Found within the Box")
            // select pin or delete etc
        }
    }
}
毁虫ゝ 2024-10-28 07:27:22

只需查看Apple的文档 MKOverlay 协议,看起来不是很复杂。只需创建一个类来执行计算覆盖所需的任何操作,并确保它符合该协议。这意味着您必须具有 coefficientboundingMapRect 属性以及 intersectsMapRect: 的实现。苹果文档解释了这一切。

看看 HazardMap 来自 Apple 的示例代码。它使用自定义叠加视图,就像您想要在叠加中显示图像一样。您只需在 MKMapViewDelegate 类中实现 mapView:viewForOverlay: 并返回 MKOverlayView 子类的新实例即可绘制自定义叠加层在屏幕上。

From just looking at Apple's documentation for the MKOverlay protocol, it doesn't look very complex. Just make a class to do whatever you need to compute the overlay and make sure it conforms to that protocol. That means you must have the coordinate and boundingMapRect properties as well as an implementation for intersectsMapRect:. The Apple docs explain it all.

Take a look at the HazardMap sample code from Apple. It uses a custom overlay view like you want to do to display an image in the overlay. You simply need to implement mapView:viewForOverlay: in your MKMapViewDelegate class and return a new instance of your subclass of MKOverlayView to get the custom overlay drawn on the screen.

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