J.Paravicini J.Paravicini - 4 months ago 14
iOS Question

How to draw routes that aren't on roads, MKMapView

So i've been experimenting with the MKMapView and overlay to create "roads that aren't on roads". That may sound weird but what I was trying to do is that in my app for iOS i want to draw the excursion routes that a specific hotel offers, however all the posts i found so far were focusing on existing roads, since the excursions go through forests, over rivers etc. there are no roads to help me.

Since there were no roads i had to improvise so I decided to make a plist(I probably could have taken another type of file too, I just liked the working with plist's) for every excursion and in there make an array of all the coordinates and getting these coordinates with google earth, but after 60 different coordinates I stopped because it was just ridiculous.

So then I tried to make a script that writes, when I tap on the map at run-time, the coordinates to the plist. Whilst this works it is still really uncomfortable, because I can't save the automatically created file in the Xcode project, and because it overall just doesn't work as good as I wished it to.

So my question is if there is something easier that I may've missed on how to create routes that aren't on streets.

Answer

Try using the site to create the gpx file.

Also note that Xcode only uses the tag so if you find a tool that generates <rte> or <trk> based gpx files, Xcode won't be able simulate your location properly. So in the file that the linked web site creates you need to change <trkpt> to <wpt>. This code below might help with getting the polyline.

func addRoute() {
  let thePath = NSBundle.mainBundle().pathForResource("Route", ofType: "gpx") // Not sure on this part
  let pointsArray = NSArray(contentsOfFile: thePath!)

  let pointsCount = pointsArray!.count

  var pointsToUse: [CLLocationCoordinate2D] = []

  for i in 0...pointsCount-1 {
    let p = CGPointFromString(pointsArray![i] as! String)
    pointsToUse += [CLLocationCoordinate2DMake(CLLocationDegrees(p.x), CLLocationDegrees(p.y))]
  }

  let myPolyline = MKPolyline(coordinates: &pointsToUse, count: pointsCount)

  mapView.addOverlay(myPolyline)
}
Comments