Reputation: 189
I am trying to use MKMapView to plot pre-determined coordinates on a map. The following code is triggered by a button.
The array 'coordinatesArr' is 2D, and contains latitude, longitude, and a timestamp. Each element is stored as a string for other reasons, and therefore co-ordinates are converted to Doubles when loaded.
Example:
[["37.33027293", "-122.02788965", "2018-05-30 15:18:21.459808+0100"], ["37.33026014", "-122.02768476", "2018-05-30 15:18:22.459808+0100"]]
The code is held within the following class:
class PostCell: UITableViewCell, MKMapViewDelegate
Code
(MAP IS THE FOLLOWING OUTLET: @IBOutlet weak var activityFeedMap: MKMapView!)
let span = MKCoordinateSpanMake(0.001, 0.001)
// ALTHOUGH THIS WILL NOT BE MY ULTIMATE METHOD, THE MAP IS CURRENTLY CENTRED ON THE FIRST LOCATION
let myLocation = CLLocationCoordinate2DMake(Double(coordinatesArr[0][1]) ?? 0.0, Double(coordinatesArr[0][0])!)
let region = MKCoordinateRegionMake(myLocation, span)
activityFeedMap.setRegion(region, animated:true)
activityFeedMap.delegate=self
activityFeedMap.mapType = MKMapType.hybrid
// CODE IN QUESTION
// ---
// PERHAPS I SHOULD LOOP THROUGH THE ARRAY, BUT I BELIEVE THIS
// FULFILS THE SAME PURPOSE
var counter=0
while counter < (coordinatesArr.count - 1){
// LASTLOCFORMATTED, AND NEXTLOCFORMATTED ARE EACH A SET OF
// CO-ORDINATES BETWEEN WHICH A POLYLINE SHOULD BE PLOTTED
let lastLocFormatted = CLLocationCoordinate2DMake(Double(coordinatesArr[counter][1])!, Double(coordinatesArr[counter][0])!)
let nextLocFormatted = CLLocationCoordinate2DMake(Double(coordinatesArr[counter+1][1])!, Double(coordinatesArr[counter+1][0])!)
// AN ARRAY HOLDING THE CO-ORDINATES OF THE POINTS TO PLOT
// A POLYLINE BETWEEN
var a=[lastLocFormatted, nextLocFormatted]
// LINE TO PLOT
let polyline = MKPolyline(coordinates: &a, count: counter)
activityFeedMap.add(polyline)
COUNTER += 1
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
// CHECK TO SEE WHETHER OVERLAY IS OF TYPE MKPOLYLINE
// AND ONLY LAY TRACE IF TIMER RUNNING
// note: different renderer required for differnt types
if overlay is MKPolyline{
// CREATE MKPOLYLINERENDERER AND PASS OUR OVERLAY OVER
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
// SET LINE COLOR TO BLUE
polylineRenderer.strokeColor = UIColor.orange
// SET LINE WIDTH TO 4
polylineRenderer.lineWidth = 4
return polylineRenderer
}
// IF OVERLAY NOT OF TYPE MKPOLYLINE
return MKPolylineRenderer()
}
Although this code is partly working, I am gaining extra data somehow.
37.33027293, -122.02788965
37.33026634, -122.0278357
37.33026525, -122.02778504
37.33026222, -122.02773507
37.33026014, -122.02768476
The above should plot:
This code creates the following, however
The line outlined in blue appears to continue on indefinitely.
Any help that you can provide would be greatly appreciated, as I am new to Swift and have exhausted all rectifying actions I can think of.
Thanks!
Upvotes: 0
Views: 167
Reputation: 6621
First off, I'd recommend using https://developer.apple.com/documentation/mapkit/mkpolyline/1452773-polylinewithpoints poloylineWithPoints instead of adding a polyline for each one.
What it looks like is your overshooting and your last segment is zooming off to Zero Island.
Upvotes: 1