将带有旋转的KML GroundOverlay 添加到MKMapView中

3
我有一个iOS应用程序,我想读取一个包含GroundOverlays的KML文件。 GroundOverlay包含图像HREF,南北东西坐标以及旋转。

我按照这个教程(http://www.raywenderlich.com/30001/overlay-images-and-overlay-views-with-mapkit-tutorial)显示地图上的图像。 我可以成功解析KML并正确显示非旋转图像。 问题是,我不知道如何显示旋转图像。

要旋转自己,我需要知道两件事:首先,如何在我的绘图代码中旋转图像。 这个问题只是关于在iOS中旋转。 其次,我需要知道KML坐标如何受到旋转的影响。 北,南,东和西坐标是旋转图像的坐标还是未旋转图像的坐标?

我已经广泛搜索了如何做到这一点的示例,但没有找到任何有用的信息。 希望提供某些实现此功能的教程/代码链接!

KML的相关部分如下:

<GroundOverlay>
  <Icon>
    <href>http://developers.google.com/kml/documentation/images/etna.jpg</href>
  </Icon>
  <LatLonBox>
    <north>37.91904192681665</north>
    <south>37.46543388598137</south>
    <east>15.35832653742206</east>
    <west>14.60128369746704</west>
    <rotation>-0.1556640799496235</rotation>
  </LatLonBox>
</GroundOverlay>

我的绘图代码(我猜旋转应该发生的地方):

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

    CGImageRef imageReference = self.overlayImage.CGImage;

    // TODO: rotate image by self.rotation degrees around center.
    MKMapRect theMapRect = self.overlay.boundingMapRect;
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);
    CGContextDrawImage(context, theRect, imageReference);
}
1个回答

2

这是我解决它的方法。首先,KML坐标是未旋转图像的坐标,因此不需要做任何事情来使它们与旋转配合使用。这里是完成的drawMapRect:zoomScale:inContext:

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

    CGImageRef imageReference = self.overlayImage.CGImage;

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

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);
    // Translate to center of image. This is done to rotate around the center, 
    // rather than the edge of the picture
    CGContextTranslateCTM(context, theRect.size.width / 2, theRect.size.height / 2);

    // _rotation is the angle from the kml-file, converted to radians.
    CGContextRotateCTM(context, _rotation);

    // Translate back after the rotation.
    CGContextTranslateCTM(context, -theRect.size.width / 2, -theRect.size.height / 2);

    CGContextDrawImage(context, theRect, imageReference);
}

我写这段代码已经有一段时间了,我并不记得比这更详细的细节,但由于有人刚刚点赞了这个问题,所以我至少想发布一下我解决它的代码。希望能对你有所帮助! :)


我也在尝试解决同样的问题,特别是在Swift中。所以,你介意在Github上分享一个例子吗?我还需要知道你是如何解析带有图像的地面叠加层的。谢谢。Github不是必需的,任何存储都可以。 - Thiha Aung
我现在明白了。感谢你的代码,我已经将它转换成了Swift。 - Thiha Aung

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接