ios - Smooth MKPolyline to follow the road -
i know has been asked before few years back, answers involved using google maps api solve this. wonder if there proper way solve ios8 out , brought many improvements native mapkit.
basically draw polyline on road, consists of many intermediate points.
var locations = [cllocation]() item in list { locations.append(cllocation(latitude: cllocationdegrees(item["location"]["coordinate"]["x"].doublevalue), longitude: cllocationdegrees(item["location"]["coordinate"]["y"].doublevalue))) } var coordinates = locations.map({(location: cllocation!) -> cllocationcoordinate2d in return location.coordinate}) var polyline = mkpolyline(coordinates: &coordinates, count: locations.count) mapview.addoverlay(polyline)
at 1 time there between 5 , 30 points on map. when polyline connects them more or less fair representation of trip. problem is: not sticks road.
so i'm getting "rough" edges time time. if using google direction api, limited 8 ways points , decides how point point b, drawing smooth polyline along way. also, directions api limited 2500 usages per 24 hours. need adjust current polyline closest road
many thanks
after digging managed find answer question, although i'm not sure impact on overall performance of whether or not going make apple happy, since sends out lot of small mkdirectionsrequest
's. me 30+ points worked fine.
var myroute : mkroute? var directionsrequest = mkdirectionsrequest() var placemarks = [mkmapitem]() item in list { var placemark = mkplacemark(coordinate: cllocationcoordinate2d(latitude: cllocationdegrees(item["location"]["coordinate"]["x"].doublevalue), longitude: cllocationdegrees(item["location"]["coordinate"]["y"].doublevalue)), addressdictionary: nil ) placemarks.append(mkmapitem(placemark: placemark)) } directionsrequest.transporttype = mkdirectionstransporttype.automobile (k, item) in enumerate(placemarks) { if k < (placemarks.count - 1) { directionsrequest.setsource(item) directionsrequest.setdestination(placemarks[k+1]) var directions = mkdirections(request: directionsrequest) directions.calculatedirectionswithcompletionhandler { (response:mkdirectionsresponse!, error: nserror!) -> void in if error == nil { self.myroute = response.routes[0] as? mkroute self.mapview.addoverlay(self.myroute?.polyline) } } } }
special goes anna pointing me in right direction.
Comments
Post a Comment