I am using Google Direction Service from Ruby.
I have this object:
params = {
origin: "37.88818,-4.77938",
destination: "36.51903,-6.28014",
waypoints: [
{
location:"Malaga",
stopover:false
}]
}
When I generate url all works fine:
uri = URI('http://maps.googleapis.com/maps/api/directions/json')
uri.query = URI.encode_www_form(params)
But when I use (lat,lng) instead place name it returns error.
params = {
origin: "37.88818,-4.77938",
destination: "36.51903,-6.28014",
waypoints: [
{
location:"36.72126,-4.42127",
stopover:false
}]
}
How I can define waypoints using (lat,lng) format as origin and destination?
Based on my understanding of the API, below are my observations
The first URL you have shown as working is working by fluke. As per documentation,
Waypoints are specified within the waypoints parameter and consist of one or more addresses or locations separated by the pipe (|) character.
Hence, The query param needed for first example is origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=Malaga
.
The effective URL will be http://maps.googleapis.com/maps/api/directions/json?origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=Malaga
.
In order to use the latitude, longitude for waypoints, you can use origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=36.72126,-4.42127
.
The effective URL will be will be http://maps.googleapis.com/maps/api/directions/json?origin=37.88818,-4.77938&destination=36.51903,-6.28014&waypoints=36.72126,-4.42
You cannot use nested Ruby Hash as input to URI#encode_www_form
as it will produce improper query string. You need to simplify your params
as shown below, so that it matches the requirements of the API
params = {
origin: "37.88818,-4.77938",
destination: "36.51903,-6.28014",
waypoints:"36.72126,-4.42"
}