As I start to use Swift 3, I've got a problem with building a route with Google Maps API directions.
My route has several waypoints and in Swift 3 the URL:
Doesn't work because of "|". Any suggestions?
As I start to use Swift 3, I've got a problem with building a route with Google Maps API directions.
My route has several waypoints and in Swift 3 the URL:
Doesn't work because of "|". Any suggestions?
For those who wants to use directions googlemaps api, you have to send your waypoints in a parameters array. So the pipe doesn't cause problem anymore.
var wayPointsString = "optimize:true"
if waypointsForRequest.count > 0 {
for location in waypointsForRequest {
wayPointsString = "\(wayPointsString)|\(location.coordinate.latitude),\(location.coordinate.longitude)"
}
}
let parameters : [String : String] = ["key" : self.directionsApikey, "sensor" : "false", "mode" : "driving", "alternatives" : "true", "origin" : "\(origin.coordinate.latitude),\(origin.coordinate.longitude)", "destination" : "\(destination.coordinate.latitude),\(destination.coordinate.longitude)", "waypoints" : wayPointsString]
let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?")
Alamofire.request(url!, method:.get, parameters: parameters)
.validate(contentType: ["application/json"])
.responseJSON { response in
if response.value != nil {
let json = JSON(response.value!)
}
}
Interesting answer here : https://stackoverflow.com/a/40126476/3173405
Hi i didn't know much about swift., but i had the same problem in objective C and I did something below and worked for me,
NSMutableCharacterSet *alphaNumSymbols = [NSMutableCharacterSet characterSetWithCharactersInString:@"~!@#$&*()-_+=[]:;',/?."];
[alphaNumSymbols formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:alphaNumSymbols];
NSURL *directionsURL = [NSURL URLWithString:urlString];
Hope this will help
in Swift,
var alphaNumSymbols = CharacterSet(charactersInString: "~!@#$&*()-_+=[]:;',/?.")
alphaNumSymbols!.formUnion(CharacterSet.alphanumerics)
urlString = urlString.addingPercentEncoding(withAllowedCharacters: alphaNumSymbols)!
var directionsURL = NSURL(string: urlString)!