Swift 2 MKMapViewDelegate rendererForOverlay compiler warning
Going by what autocomplete suggests the prototype looks like this:
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
And apparently there's nothing you can do about it, except for returning return MKPolylineRenderer()
where normally you would return nil.
To me it looks like an implementation bug, because here's what the documentation says about the returned object:
The renderer to use when presenting the specified overlay on the map. If you return nil, no content is drawn for the specified overlay object.
I suggest you create a case for it in Apple's bug report
How can I fix my MKOverlayRenderer now that I'm using Swift 2.0?
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
will only be called for each overlay that you add to your instance of MKMapView.
If you only add an MKPolyline
to your mapView, you can be sure that when this delegate function is called, the overlay
parameter will be of type MKPolyline
. Since this is the case, there is no reason to check if the overlay parameter is an instance of MKPolyline
with if overlay is MKPolyline
. You can just do this:
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let pr = MKPolylineRenderer(overlay: overlay)
pr.strokeColor = self.lightBlue
pr.lineWidth = 14
return pr
}
If you ever add a different type of overlay to your MKMapView, you can do this:
if overlay is MKPolyline {
// return renderer for MKPolyline overlay
return mkPolylineRenderer
} else {
// handle the different type of overlay...
return otherTypeOfOverlayRenderer
}
The Swift compiler is intelligent enough to infer from the if/else block that something will always be returned.
MKPolyLine not appearing on MapView in Swift
As posted in the comments, the code in the question was fine. I simply wasn't setting the delegate.
mapView.delegate = self
MapKit overlay does not appear
See the accepted answer to this question:
How to create MKCircle in Swift?
Steps are:
Ensure this controller implements
MKMapViewDelegate
:class YourViewController: UIViewController, MKMapViewDelegate
In
viewDidLoad
set the delegate on map to self:override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self;`Add the following function to render the overlay (change the color, etc):
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if let overlay = overlay as? MKCircle {
let circleRenderer = MKCircleRenderer(circle: overlay)
circleRenderer.fillColor = UIColor.blueColor()
return circleRenderer
}
else {
return MKOverlayRenderer(overlay: overlay)
}
}The radius of your circle is too big as it is currently set and the whole map will be blue. Set it to something small, like 1000, so you can see the circle.
Polyline Overlay in Swift
I think issue here is with the line:
var a = [c1, c2]
Here you directly created array without specifying its type.
See below reference code to create Polyline overlay and related delegate method:
let c1 = myCLLocationCoodinate
let c2 = myCLLocationCoodinate2
var points: [CLLocationCoordinate2D]
points = [c1, c2]
var geodesic = MKGeodesicPolyline(coordinates: &points[0], count: 2)
mapView.add(geodesic)
UIView.animate(withDuration: 1.5, animations: { () -> Void in
let span = MKCoordinateSpanMake(20, 20)
let region1 = MKCoordinateRegion(center: c1, span: span)
mapView.setRegion(region1, animated: true)
})
A delegate method to render overlay:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.whiteColor()
polylineRenderer.lineWidth = 2
return polylineRenderer
}
return nil
}
Related Topics
Xcode 9 Fails to Build Swift 4 Project with Pod
Display Table View When Searchbar (From Searchcontroller) Begin Edited Swift
Using Codable to Encode/Decode from Strings to Ints with a Function in Between
Swift 4.2 - _Shared Attribute Near Type
Accessing Global Variable in Swift
Usage of Withmemoryrebound with Apples Swift 3 Beta 6
Name Convention for Unwrapped Value in Swift
How to Delete Item from Collection View
Swift 2: Invalid Conversion from Throwing Function of Type to Non-Throwing Function
How to Get Core Data Entity by It's Objectid
How to Add Openssl to a Swift Project
Changing the Color of the Icons in a Uitextfield Inside a Uisearchbar
Ios-Charts Set Maximum Visible X Axis Values
Swiftui Pick a Value from a List with Ontap Gesture
How to Set a Function as Function Argument in Swift 3
Table View Cellforrowatindexpath Warning
Swift 'Unable to Dequeue a Cell with Identifier Intervalcellidentifier