1

I created a custom tile service and add it to a mapView then it works fine. then I used the same code and created a framework library with a MapView return type in Xcode 8. then I used a sample test app and import that library to it and I called the method used in library and add it to a mapView. So my problem Is when I call and that method to mapView it displays the MapKit map not my custom map

code used in library

import Foundation
import MapKit
public class mapLib: NSObject{

    public  class func createMap(mapView: MKMapView) ->MKMapView{
        let mapView = mapView
        //custom map URL
        let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
        let overlay = MKTileOverlay(urlTemplate: template)
        overlay.canReplaceMapContent = true
        mapView.add(overlay, level: .aboveLabels)
        return mapView;
    }
}

code used in app

import UIKit
import MapKit
import mapLib
class ViewController: UIViewController {
    @IBOutlet weak var mapV: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let view = mapLib.createMap(mapView: mapV)
        mapV.addOverlays(view.overlays)

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

I need to clarify that the way I'm going to approach would work or any other method to do it :)

Nishant Bhindi
  • 2,242
  • 8
  • 21

1 Answers1

1

You are missing add self as delegate and implement func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer method, you can do this in your library

something like this

import Foundation
import MapKit
public class mapLib: NSObject{

    public  class func createMap(mapView: MKMapView) ->MKMapView{
        let mapView = mapView
        //custom map URL
        let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
        let overlay = MKTileOverlay(urlTemplate: template)
        overlay.canReplaceMapContent = true
        mapView.add(overlay, level: .aboveLabels)
        mapView.delegate = self
        return mapView;
    }
}

extension mapLib : MKMapViewDelegate{

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if let overlayTile = overlay as? MKTileOverlay{
            let overLayRenderer = MKTileOverlayRenderer(tileOverlay: overlayTile)
            return overLayRenderer
        }

        return MKOverlayRenderer(overlay: overlay)
    }
}
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55