Reputation: 3224
I am using "google direction matrix api" to generated direction route between two address points including waypoints. To take address input from user, I am using google's Place Autocomplete.
var options = {
componentRestrictions: {country: 'UK'}
};
var fromText = document.getElementById('start');
var fromAuto = new google.maps.places.Autocomplete(fromText,options);
Issue : This happen only with some addresses
When user start typing like terminal and select address from google suggested address "Terminal 2, Hounslow, United Kingdom"
Suppose this is Source address and another address is "London, UK". After filling these addresses and when I click on "Generate Map" button. An error popup "Direction request failed due to NOT_FOUND".
But if I change address "Terminal 2, Hounslow, United Kingdom" to "Terminal Two, Hounslow, United Kingdom" manually (changing numeric value "2" to alphabet "two"). It works fine.
I dont know why this is happen ? while "Terminal 2, Hounslow, United Kingdom" is auto suggested by the Google.
Any help would be appreciated
Upvotes: 0
Views: 280
Reputation: 32168
As you are using the autocomplete input, I can suggest using the places IDs in Distance Matrix requests instead of the texts.
When user selects a place you can store the corresponding place ID and use it later in distance matrix calculation.
E.g.
will return place ID ChIJu8J5RChydkgROPKSCZojxCg
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=London%2C%20UK&key=YOUR_API_KEY
will return place ID ChIJdd4hrwug2EcRmSrV3Vo6llI
Now execute Distance Matrix request for place IDs:
The response is:
{
"destination_addresses":[
"London, UK"
],
"origin_addresses":[
"London Heathrow Terminal 2, Compass Centre, Nelson Road, Longford, Hounslow TW6 2GW, UK"
],
"rows":[
{
"elements":[
{
"distance":{
"text":"28.7 km",
"value":28661
},
"duration":{
"text":"49 mins",
"value":2940
},
"status":"OK"
}
]
}
],
"status":"OK"
}
You can do the similar thing in Maps JavaScript API. Please have a look at this example that calculates routes using the directions service with place IDs:
http://jsbin.com/xuyisem/1/edit?html,output
You can easily apply this approach for distance matrix.
Hope it helps!
Upvotes: 2