首页 > 解决方案 > 如何将图像叠加层添加到 MKMapView?

问题描述

----------已更新------------
底部的原始问题


我已经走得很远了,我现在有这个:

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet var mapView: MKMapView!

    var locationManager: CLLocationManager!
    var mapOverlay: MKOverlay!

    override func viewDidLoad() {
        super.viewDidLoad()

        var points = [CLLocationCoordinate2D(latitude: -29.8122, longitude: 148.6351),
                      CLLocationCoordinate2D(latitude: -27.9307, longitude: 148.6351),
                      CLLocationCoordinate2D(latitude: -27.9307, longitude: 150.9909),
                      CLLocationCoordinate2D(latitude: -29.8122, longitude: 150.9909)]
        let tile = MKPolygon(coordinates: &points, count: points.count)
        tile.title = "zurich"
        mapView.addOverlay(tile)

        //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.
    }
    // mapView delegate function
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let renderer = MKPolygonRenderer(overlay: overlay)
        renderer.fillColor = UIColor.red
        return renderer
    }
}

我现在需要知道如何renderer.fillColor = UIColor.red用可以显示我的图像的东西来替换它。再次感谢
-----原始问题-----

所以,我是 Swift 和 MapKit 的新手,我想在 MKMapView 上添加一个简单的图像叠加层。我找到了一些答案,但它们都令人困惑,而且它们都适用于 Swift 3 及更早版本。

我发现地图视图需要一个委托,那是一个文件吗?我已经使用主视图控制器创建了一个地图视图。

这是我到目前为止所做的(在 ViewController.swift 文件中):

import UIKit
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate
{
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    }

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib

        let location = CLLocationCoordinate2D(latitude: 47.457925,
                                              longitude: 8.548466)

        let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
        let region = MKCoordinateRegion(center: location, span: span)
        mapView.setRegion(region, animated: true)


    }


}

谢谢你,我希望你能帮忙!

标签: swiftmapkit

解决方案


有很多方法可以将图像嵌入到地图中。

  1. 注释视图
  2. 标注
  3. 自定义地图图块

更多地解释您的需求,也许我们可以更好地帮助您到达那里。


您正在地图上添加叠加层。我们想用特定的地图图块来改变。

func createLocalUrl(forImageNamed name: String) -> URL? {

    let fileManager = FileManager.default
    let cacheDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
    let url = cacheDirectory.appendingPathComponent("\(name).png")

    guard fileManager.fileExists(atPath: url.path) else {
        guard
            let image = UIImage(named: name),
            let data = image.pngData()
            else { return nil }

        fileManager.createFile(atPath: url.path, contents: data, attributes: nil)
        return url
    }

    return url
}

func setupTiles() {
    let url = createLocalUrl(forImageNamed: "yourImageName")
    let template = url?.absoluteString
    let overlay = MKTileOverlay(urlTemplate: template)
    overlay.canReplaceMapContent = true
    self.tileOverlay = overlay
    mapView.addOverlay(overlay)
    self.tileRenderer = MKTileOverlayRenderer(tileOverlay: overlay)
}

func isInDesiredArea(middlePoint: MKMapPoint) -> Bool {

    //mapView has convert function which converts CGPoint -> 
    //CLLocationCoordinate2D and vice versa Use this function and,
    //Your polygon has boundingMapRect which has contains function.
    //Also your map has func mapView(_ mapView: MKMapView, 
    //regionDidChangeAnimated animated: Bool) which runs whenever region changes..
    return myBoundsPolygon.boundingMapRect.hasContain(middlePoint)

}


func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
 //Convert middle point of your view to CLLocationCoordinate2D
 //Convert your coordinate to MKMapPoint
 if isInDesiredArea(middlePoint: point) {
     setupTiles()
 }
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    ....
if overlay is MKTileOverlay {
    return tileRenderer
} 

推荐阅读