Reputation: 127
I am trying to build request for google matrix service which will return distances between origin point and destinations.
Following : Google Docs
Can not figure out how to build correct request.
SUCCESS: {
"destination_addresses" = (
);
"origin_addresses" = (
);
rows = (
);
status = "INVALID_REQUEST";
}
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&destinations%5B%5D=30.37577%2C-81.7837&destinations%5B%5D=26.89648%2C-82.00874&destinations%5B%5D=30.48638%2C-84.42159&destinations%5B%5D=35.11241%2C-80.95421&destinations%5B%5D=33.82343%2C-84.12118&destinations%5B%5D=33.67012%2C-78.93803&destinations%5B%5D=39.87034%2C-82.94822&destinations%5B%5D=42.14669%2C-87.83312&destinations%5B%5D=42.65981%2C-84.55101&destinations%5B%5D=39.87016%2C-82.9482&destinations%5B%5D=39.86784%2C-82.94884&destinations%5B%5D=39.87014%2C-82.94824&destinations%5B%5D=39.87021%2C-82.94826&destinations%5B%5D=42.67464%2C-82.83249&destinations%5B%5D=39.87025%2C-82.94812&destinations%5B%5D=39.87049%2C-82.94807&destinations%5B%5D=39.87057%2C-82.94829&destinations%5B%5D=40.43237%2C-74.48872&destinations%5B%5D=39.96912%2C-82.99415&destinations%5B%5D=40.44534%2C-74.41769&destinations%5B%5D=40.4453%2C-74.41779&destinations%5B%5D=41.28482%2C-72.93002&destinations%5B%5D=40.44524%2C-74.41776&destinations%5B%5D=40.44559%2C-74.41811&destinations%5B%5D=42.12788%2C-75.97026&destinations%5B%5D=40.44576%2C-74.41788&destinations%5B%5D=40.69494%2C-74.09928&destinations%5B%5D=40.76857%2C-73.73745&destinations%5B%5D=40.4856%2C-74.39982&destinations%5B%5D=30.26164%2C-98.8715&destinations%5B%5D=39.95258%2C-82.90484&destinations%5B%5D=32.69126%2C-96.24597&destinations%5B%5D=29.48223%2C-98.34786&destinations%5B%5D=32.82934%2C-97.03087&destinations%5B%5D=32.8292%2C-97.03081&key=NOPE_THATS_MY_KEY&origins=37.6724723880042%2C-90.4441829064476
The way I build the destinations array :
func createDestinationsArray(annotations: [MyAnnotation]) -> [String] {
var destinations: [String] = []
var destination: String!
for annotation in annotations {
destination = "\(annotations.lat),\(annotations.lon)"
destinations.append(destination)
}
return destinations
}
And the request :
let queue = DispatchQueue(label: "com.cnoon.distance-queue", qos: .utility, attributes: .concurrent)
Alamofire.request(url, method: .get, parameters: ["origins" : origins, "destinations" : destinations, "key" : apiKey]).response(queue: queue, responseSerializer: DataRequest.jsonResponseSerializer(), completionHandler: { response in
...
})
Thanks in advance!!
Upvotes: 0
Views: 189
Reputation: 54716
The docs clearly show that you should have |
as separating characters between the elements of your arrays. Also, you append destinations in front of each coordinate pair, which is clearly wrong if you look at the docs. You have two options. Either create your destinations and origins arrays as strings on the first place, containing the separators or later join the arrays into strings with the separator. Since as far as I can tell, you won't need your arrays for anything but the network request, I think generating a string directly is the more optimal solution.
func createDestinationsArray(annotations: [MyAnnotation]) -> String {
return annotations.map({ return "\($0.latitude),\($0.longitude)" }).joined(separator: "|")
}
Upvotes: 0