如何在Swift 2中创建图像叠加层并将其添加到MKMapView? [英] How do I create an image overlay and add to MKMapView in Swift 2?

查看:115
本文介绍了如何在Swift 2中创建图像叠加层并将其添加到MKMapView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习MapKit,现在正在尝试添加图像(而不是多边形,圆形,矩形等)作为地图视图的叠加层.我似乎找不到任何示例代码来帮助解释如何执行此操作.

I am trying to learn MapKit and am now trying to add an image (not a polygon, circle, rectangle, etc.) as an overlay to the map view. I can't seem to find any sample code to help explain how to do this.

到目前为止,我在ViewController.swift中的代码是:

So far my code in ViewController.swift is:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet var mapView: MKMapView!

    var locationManager: CLLocationManager!
    var mapOverlay: MKOverlay!

    override func viewDidLoad() {
        super.viewDidLoad()

        //Setup our Location Manager
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()

        //Setup our Map View
        mapView.delegate = self
        mapView.mapType = MKMapType.Satellite
        mapView.showsUserLocation = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

我知道我需要使用:

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer

和drawMapRect函数,但是我不知道要放什么.另外,如果所有代码都在ViewController.swift

and the drawMapRect function but I don't know what to put in them. Also, it would be useful if all the code is in ViewController.swift

我只需要一个覆盖,所以我不需要上课.我需要它是图像叠加层(以便可以调整大小),而不是注释.我还尝试按照 http:/的指南进行操作. /www.raywenderlich.com/30001/overlay-images-and-overlay-views-with-mapkit-tutorial ,但其中没有包含所有代码的部分,我很难遵循.大家能帮我如何创建 image MKOverlays并将其添加到MKMapKit吗?

I only need one overlay so I don't need a class. I need it to be an image overlay (so it can resize), not an annotation. I also tried to follow the tutorial at http://www.raywenderlich.com/30001/overlay-images-and-overlay-views-with-mapkit-tutorial but there is not a section with all the code and I find it hard to follow along. Can you people please help me how to create image MKOverlays and add them to MKMapKit?

预先感谢...

推荐答案

Swift 3

以下内容在Swift 3中对我有用.

The following works for me in Swift 3.

class ImageOverlay : NSObject, MKOverlay {

    let image:UIImage
    let boundingMapRect: MKMapRect
    let coordinate:CLLocationCoordinate2D

    init(image: UIImage, rect: MKMapRect) {
        self.image = image
        self.boundingMapRect = rect
        self.coordinate = rect.centerCoordinate
    }
}

class ImageOverlayRenderer : MKOverlayRenderer {

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {

        guard let overlay = self.overlay as? ImageOverlay else {
            return
        }

        let rect = self.rect(for: overlay.boundingMapRect) 

        UIGraphicsPushContext(context)
        overlay.image.draw(in: rect)
        UIGraphicsPopContext()
    }
}

然后像往常一样,在您的MapView委托中:

Then as usual, in your MapView delegate:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

    if overlay is ImageOverlay {
        return ImageOverlayRenderer(overlay: overlay)
    }
    ...
}

以及每当需要在地图上添加图像时:

And whenever you need to add an image to your map:

let overlay = ImageOverlay(image: myImage, rect: desiredLocationAsMapRect)
mapView.add(overlay)

请注意,我始终确保rect具有与图像相同的宽高比.否则,我怀疑会导致图像失真.

Note, I always ensure that the rect has the same aspect ratio as the image. Otherwise, I suspect image distortion will result.

这篇关于如何在Swift 2中创建图像叠加层并将其添加到MKMapView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆