How to use MKPolylineView in Swift
First, instead of MKPolylineView
, you must create an MKPolylineRenderer
.
MKPolylineView
has been deprecated since iOS 7 and, although you can still use it in Objective-C if necessary, it's not supported in Swift.
Second, you must create and return an MKPolylineRenderer
in the rendererForOverlay
delegate method (not pass it to addOverlay
).
In the addOverlay
method, you pass the MKPolyline
object (not the MKPolylineView
or MKPolylineRenderer
).
(See Adding MKOverlayPathRenderer as overlay to MKMapView gets exception for an explanation of the difference between what object you pass to addOverlay
vs. what object you return in rendererForOverlay
.)
So in the setMapView
method, remove the lines that create and set myPolylineView
and change the addOverlay
line to:
self.theMapView.addOverlay(polyline)
Then implement the rendererForOverlay
method:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blueColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
Make sure the map view's delegate
is set otherwise the delegate method won't get called and the overlay will not appear. If theMapView
is an IBOutlet, connect the delegate
outlet or set it in code (eg. in viewDidLoad
after calling super):
self.theMapView.delegate = self
Rendering MKPolyline in Swift
You may need in viewDidLoad
self.mapView.delegate = self
Swift, MKPolyline how to create polyline between coordinate point
You're creating multiple polylines with a single point, instead of a single polyline with multiple points. It's hard to get the code right without knowing your dictionary structure, but this should be more in line with what you're trying to do:
var arrayToDraw: Array<Any> = []
var forpolyline: Array<CLLocationDegrees> = []
var forpolyline2: CLLocationCoordinate2D = [CLLocationCoordinate2D]()
func showRoute() {
for h in 0...(orderFinalDictionary.count - 1){
arrayToDraw = orderFinalDictionary[h].value
print(arrayToDraw)
var arrayToDrawCount = arrayToDraw.count
for n in 0...(arrayToDrawCount - 1){
forpolyline = (arrayToDraw[n] as! Array<CLLocationDegrees>)
forpolyline2.append(CLLocationCoordinate2D(latitude: forpolyline[0], longitude: forpolyline[1]))
}
print(forpolyline2)
let geodesic = MKPolyline(coordinates: &forpolyline2, count: forpolyline2.count)
self.mapView.add(geodesic)
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 3
return renderer
}
Drawing MKPolyline to be more accurate
You will get an updated location every time it changes, and you can either build up an array of locations, or store them in a database for later retrieval.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
// the last location presented here is the current position of the runner
self.locations.append(locations.last)
// store this in a database, or as a class variable
}
You can set the desired accuracy when you initialise your location manager
locationManager.desiredAccuracy = kCLLocationAccuracyBest
and then when you're creating the map, create a line showing the runner's route
let polyline = MKPolyline(coordinates: self.locations, count: self.locations.count)
self.mapView.addOverlay(polyline)
Here's an example of the output - looks like the finish is in the middle of the run, but that's because I was heading back home
How to add annotations at a certain distance along a MKPolyline route using iOS MapKit in Swift?
Treat your poly line points as a sequence of line segments. Start out with the target distance, 13 miles. Iterate over the points as line segments - .windows(ofCount: 2), compute the length of that line segment, if remaining distance is < line segment length, then your target point is the point that is 'remaining distance' from start of line segment in the direction of end of line segment. Otherwise, subtracting the line segment's length from remaining distance, until either you find your target or fall off the end of the poly line. The question of whether each line segment on poly line represents straight line (along map projection) or great circles (on surface of earth) is unimportant unless the distance between points is large.
Related Topics
How to Find the Kind of Errors a Method May Throw and Catch Them in Swift
How to Prevent a Remote Notification from Being Displayed
How to Parse Array of JSON to Array in Swift
Pinch to Zoom Effect on Uiimageview Inside Scrollview
How to Start an Ibeacon Transmitter in the Background
Restore Button for iOS Non-Consumable In-App Purchase
Dismiss a Parent Modal in Swiftui from a Navigationview
Building Ffmpeg iOS Libraries for Armv7, Armv7S, Arm64, I386 and Universal
How to Make a Mkannotationview Touch Sensitive
Gssendevent - Inject Touch Event iOS
Uitableview Pagination - Bottom Refresh to Load New Data in Swift
How to Segue with Data from One Tab to Another Tab Properly
Cannot Invoke Initializer for Type 'Double' with an Argument List of Type '(String)'
Uisearchcontroller Disable Cancel Uibarbuttonitem