【问题标题】:Adding GroundOverlay from KML to MKMapView with rotation通过旋转将 GroundOverlay 从 KML 添加到 MKMapView
【发布时间】:2013-06-27 14:53:36
【问题描述】:

我有一个 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);
}

【问题讨论】:

    标签: ios mkmapview kml


    【解决方案1】:

    这就是我最终解决它的方法。首先,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 是不必要的。欢迎任何存储。
    • 我现在知道了。感谢您的代码,我将它变成了 swift。
    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 2012-06-07
    • 2015-04-26
    • 2011-03-04
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多