1
votes

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:

https://maps.googleapis.com/maps/api/directions/json?origin=48.4843822562792,35.0635632500052&destination=48.4893899423081,35.0640017911792&waypoints=48.4833428800255,35.0710221379995|48.4887622031403,35.0573639944196&key=AIzaSyAWpBT3uxovKLqdWIiwa29a4AcgtspAA1k

Doesn't work because of "|". Any suggestions?

3
what the problem with swift 3 ??? its an pure URL not related to any language.so may i know what was the problem ? - CodeChanger
on swift 2.3 with alamofire this request works as expected, but now I'm getting only one "leg" in response, though I'm sending several waypoints - Alexandr Kolesnik
Have you log that url which you are requesting ? is it same as mention in question or differ from original one ? - CodeChanger
yes, I got different results in browser and in my app - Alexandr Kolesnik
so its breaking your URL look into your code some ware its breaking your URL string. - CodeChanger

3 Answers

2
votes

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

0
votes

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)!
0
votes

In Swift 3, the following helped

let url_string = "URL STRING"
let url = URL(string:url_string.addingPercentEncoding(withAllowedCharacters:  CharacterSet.urlQueryAllowed))