ios - How to draw a proper polylines on google maps -
i have drawn polyline point point b, reason , shows linear line not correct waypoints
here's code
let path = gmsmutablepath() path.addlatitude(3.1970044, longitude:101.7389365) path.addlatitude(3.2058354, longitude:101.729536) let polyline = gmspolyline(path: path) polyline.strokewidth = 5.0 polyline.geodesic = true polyline.map = mapview
i expecting doing waypoints, shows straight polylines
self.googlemapsview google maps view.
example : self.getdirections("26.9211992,75.8185761", destination: "26.8472496,75.7691909", waypoints: ["26.8686811,75.7568383"], travelmode: nil, completionhandler: nil)
example: google direction link https://maps.googleapis.com/maps/api/directions/json?origin=26.9211992,75.8185761&destination=26.8472496,75.7691909&waypoints=optimize:true|26.8686811,75.7568383
let baseurlgeocode = "https://maps.googleapis.com/maps/api/geocode/json?" let baseurldirections = "https://maps.googleapis.com/maps/api/directions/json?" var selectedroute: dictionary<nsobject, anyobject>! var overviewpolyline: dictionary<nsobject, anyobject>! var origincoordinate: cllocationcoordinate2d! var destinationcoordinate: cllocationcoordinate2d! func getdirections(origin: string!, destination: string!, waypoints: array<string>!, travelmode: anyobject!, completionhandler: ((status: string, success: bool) -> void)?) { if let originlocation = origin { if let destinationlocation = destination { var directionsurlstring = baseurldirections + "origin=" + originlocation + "&destination=" + destinationlocation if let routewaypoints = waypoints { directionsurlstring += "&waypoints=optimize:true" waypoint in routewaypoints { directionsurlstring += "|" + waypoint } } print(directionsurlstring) directionsurlstring = directionsurlstring.stringbyaddingpercentescapesusingencoding(nsutf8stringencoding)! let directionsurl = nsurl(string: directionsurlstring) dispatch_async(dispatch_get_main_queue(), { () -> void in let directionsdata = nsdata(contentsofurl: directionsurl!) do{ let dictionary: dictionary<nsobject, anyobject> = try nsjsonserialization.jsonobjectwithdata(directionsdata!, options: nsjsonreadingoptions.mutablecontainers) as! dictionary<nsobject, anyobject> let status = dictionary["status"] as! string if status == "ok" { self.selectedroute = (dictionary["routes"] as! array<dictionary<nsobject, anyobject>>)[0] self.overviewpolyline = self.selectedroute["overview_polyline"] as! dictionary<nsobject, anyobject> let legs = self.selectedroute["legs"] as! array<dictionary<nsobject, anyobject>> let startlocationdictionary = legs[0]["start_location"] as! dictionary<nsobject, anyobject> self.origincoordinate = cllocationcoordinate2dmake(startlocationdictionary["lat"] as! double, startlocationdictionary["lng"] as! double) let endlocationdictionary = legs[legs.count - 1]["end_location"] as! dictionary<nsobject, anyobject> self.destinationcoordinate = cllocationcoordinate2dmake(endlocationdictionary["lat"] as! double, endlocationdictionary["lng"] as! double) let originaddress = legs[0]["start_address"] as! string let destinationaddress = legs[legs.count - 1]["end_address"] as! string let originmarker = gmsmarker(position: self.origincoordinate) originmarker.map = self.googlemapsview originmarker.icon = gmsmarker.markerimagewithcolor(uicolor.greencolor()) originmarker.title = originaddress let destinationmarker = gmsmarker(position: self.destinationcoordinate) destinationmarker.map = self.googlemapsview destinationmarker.icon = gmsmarker.markerimagewithcolor(uicolor.redcolor()) destinationmarker.title = destinationaddress if waypoints != nil && waypoints.count > 0 { waypoint in waypoints { let lat: double = (waypoint.componentsseparatedbystring(",")[0] nsstring).doublevalue let lng: double = (waypoint.componentsseparatedbystring(",")[1] nsstring).doublevalue let marker = gmsmarker(position: cllocationcoordinate2dmake(lat, lng)) marker.map = self.googlemapsview marker.icon = gmsmarker.markerimagewithcolor(uicolor.purplecolor()) } } let route = self.overviewpolyline["points"] as! string let path: gmspath = gmspath(fromencodedpath: route)! let routepolyline = gmspolyline(path: path) routepolyline.map = self.googlemapsview } else { print("status") //completionhandler(status: status, success: false) } } catch { print("catch") // completionhandler(status: "", success: false) } }) } else { print("destination nil.") //completionhandler(status: "destination nil.", success: false) } } else { print("origin nil") //completionhandler(status: "origin nil", success: false) } }
Comments
Post a Comment