用静态图像替换MKMapView

Emi*_*Moe 3 mkmapview ios ios7

是否可以显示静态图像而不是默认的MapView,其中当前位置始终是图像的中心?

我想显示中心为当前位置的图像,并根据坐标(距离和方向)在其上添加引脚.我想计算它之间的距离,也许根据手机指向的方向旋转图像/引脚.

我认为MKMapView使用静态图像替换它可能是最容易的,因为我可以使用所有内置功能,但是现在似乎无法将地图更改为静态图像?

我也可以直接在图像上绘画,但是如何工作,我应该这样做吗?我想这将是极坐标的东西.

Leo*_*ica 8

使用iOS7,您MKMapSnapshotter可以在后台线程中渲染地图区域的快照.然后,您可以拍摄该快照的图像.

MKMapSnapshotOptions* options = [MKMapSnapshotOptions new];
//Setup options here:
options.camera = ...
options.region = ...

MKMapSnapshotter* snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionHandler:^(MKMapSnapshot* snapshot, NSError* error) {

    // get the image associated with the snapshot

    UIImage *image = snapshot.image;

    dispatch_async(dispatch_get_main_queue(), ^ {
        //Make sure to access UIKit UI elements on the main thread!
        [self.imageView setImage: image];
    });
}
Run Code Online (Sandbox Code Playgroud)


Mor*_*sen 5

您可以根据需要使用Google的静态地图API。那很简单。这是来自丹麦哥本哈根的某处的静态图片:

NSData *data = [NSData dataWithContentsOfURL:@"http://maps.googleapis.com/maps/api/staticmap?center=55.675861+12.584574&zoom=15&size=400x400&sensor=false"];
UIImage *img = [UIImage imageWithData:data];
Run Code Online (Sandbox Code Playgroud)

然后,您可以根据需要添加标记- 在此处了解如何添加标记。这是一个用于在中间添加带有文本“ M”的红色标记的测试URL:

http://maps.googleapis.com/maps/api/staticmap?center=55.675861+12.584574&zoom=15&​​size=400x400&sensor=false&markers=color:red%7Clabel:M%7C55.675861+12.584574

解码URL的标记部分:

标记=颜色:红色%7C标签:M%7C55.675861 + 12.584574

你得到这个:

标记=颜色:红色|标签:M | 55.675861 12.584574

编辑:

是一种刮取地图控件图像的方法。如果我们提取答案的重要部分,这基本上就是您可以做到的:

UIGraphicsBeginImageContextWithOptions(map.bounds.size, map.opaque, 0.0);
[map.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

请注意,map需要从派生UIView,这意味着您可以在各种控件上使用此技巧。

编辑2:

您还应该看看这篇文章。确实写得很好,涵盖了许多与此相关的主题,包括覆盖层,图钉等。