How to create image overlay and add to MKMapView in Swift 2?

I am trying to learn MapKit and am now trying to add an image (rather than a polygon, circle, rectangle, etc.) as an overlay on the map view. I cannot find any sample code to explain how to do this.

So far my code in ViewController.swift:

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.
    }

}

I know what I need to use:

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

and drawMapRect function, but I don’t know what to enter. It would also be helpful if all the code was in ViewController.swift

, . , ( ), . http://www.raywenderlich.com/30001/overlay-images-and-overlay-views-with-mapkit-tutorial, , . , , MKOverlays MKMapKit?

...

+4
2

Swift 3

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:

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

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

, :

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

. , . , , .

+2

Apple , Objective-C, Swift.

, ( ). - , - 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 = "Moree"
    mapView.addOverlay(tile)

, points var, .

:

// mapView delegate function
  func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {

    if overlay.isKindOfClass(MKPolygon) {
      let renderer = MKPolygonRenderer(overlay: overlay)

      renderer.fillColor = UIColor.cyanColor().colorWithAlphaComponent(0.2)
      renderer.strokeColor = UIColor.blueColor().colorWithAlphaComponent(0.7)
      renderer.lineWidth = 3
      return renderer
    }
    fatalError()
  }

Swift 2.1/Xcode 7.2, . , /lons, . .

+1

Source: https://habr.com/ru/post/1624801/


All Articles