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
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.
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Terminal%202%2C%20Hounslow%2C%20United%20Kingdom&components=country%3AGB&key=YOUR_API_KEY
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:
https://maps.googleapis.com/maps/api/distancematrix/json?origins=place_id%3AChIJu8J5RChydkgROPKSCZojxCg&destinations=place_id%3AChIJdd4hrwug2EcRmSrV3Vo6llI&key=YOUR_API_KEY
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!
Related
I have a weird problem when dealing with Google Maps Autocomplete / Places API, it autocompletes the given address just fine:
Street Streetnumber, City, Country = Römerstraße 101, Reith bei Seefeld, Österreich
With the autocompleted value, I am trying to get the address_component data.
const autocomplete = new this.api.places.Autocomplete(
options.input,
{types: ['address'], sessiontoken: sessionToken}
),
inputObj = $(options.input);
inputObj.prop('placeholder', inputObj.data('geocode-autocomplete-placeholder'));
autocomplete.setFields(['address_component']);
autocomplete.addListener('place_changed', () => {
try {
callback(options, {'queryResult': {'res': [autocomplete.getPlace()]}}, this);
} catch (e) {
this.disableGeocoding();
console.log(e);
}
});
Problem is with the returned result, where "city" is missing:
I would expect every information that has been autocompleted to be in the "address_components" data.
Questions:
Why is city missing, and not found in another node, although autocompleted?
How can/should i get the city information?
According to these two links,
https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
https://developers.google.com/places/web-service/details
It says locality of response is represents the city part of the address.
So you can see locality part in the response.
Refer those links to get more information.
I am using google auto complete API to search address. Some time after selecting the address it returns wrong latitude and longitude. Then I tried second time its working fine. What could be the cause for this?
The address tried: 21 King Street West, Toronto, ON, Canada
First time the Geo-code coming as below
40.7917804,-74.14482129999999 - wrong (USA Address)
Second time I am getting correct Geo-code
43.64877740000001,-79.37871480000001
var address = new google.maps.places.Autocomplete((document.getElementById(id)),{ types: ['geocode'], componentRestrictions: { country: 'ca' } });
address.addListener('place_changed', function () {
var place = address.getPlace();
place = results[0];
$j('#' + txtboxid).attr({
'data-lat': place.geometry.location.lat(),
'data-lon': place.geometry.location.lng()
});
});
Try changing the type to address which is a precise location for premises: types: ['address']
I am stumped at the moment with the Google Geocoder. Right now, if I query by address, "New York City, NY", I get back results that say the types of the result is "types":["locality","political"] but there is no locality in the address_components. If I query "Tempe, AZ", same thing. If I search for a state, the types is administrative_area_level_1 but it only gives me the country in the address_compenents. Any ideas?
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': "New York City, NY"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//do stuff
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
My results for "New York City, NY"
[{"address_components":[{"long_name":"New York","short_name":"NY","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"New York, USA","geometry":{"bounds":{"south":40.4773991,"west":-74.25908989999999,"north":40.9175771,"east":-73.7002721},"location":{"lat":40.7127837,"lng":-74.00594130000002},"location_type":"APPROXIMATE","viewport":{"south":40.4960439,"west":-74.2557349,"north":40.9152555,"east":-73.7002721}},"place_id":"ChIJOwg_06VPwokRYv534QaPC8g","types":["locality","political"]}]
I am confused as why it is telling me it is type city (locality), but missing the locality attribute. I would expect the locality to be there with the value, "New York" and the formatted address to include the locality in it. Thanks!
Never assume that "locality", or any other type, will always be present in address components.
See gmaps-api-issues #3320 and the longer explanation in the Place Autocomplete Address Form example.
If you really need a specific component, reverse geocoding is the only reliable way to get it always (that is, in places where it actually exists).
I've been searching for solutions in this forum but I cannot find any:
I cannot find my business place ID, which I need for the google reviews snippet.
I type my business name in the placeID finder and nothing shows up. I can only look up the address of the building where my business is, but not my actual business.
my business page:
https://business.google.com/b/106802106987470032893/dashboard/l/06489107232401690502?pageId=106802106987470032893
my google maps link: https://www.google.com/maps/place/CherryLaser+laser+cutting+and+engraving+services/#34.0454354,-118.2509478,17z/data=!4m12!1m6!3m5!1s0x80c2c63593d2cbab:0x9417c55fe89ca04b!2sCherryLaser+laser+cutting+and+engraving+services!8m2!3d34.0454354!4d-118.2487591!3m4!1s0x80c2c63593d2cbab:0x9417c55fe89ca04b!8m2!3d34.0454354!4d-118.2487591
I'm also trying to get hold of google map api support or any human at google, with no luck. Anybody knows where I can find the customer support number?
Thanks
I executed Places API text search request:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=CherryLaser%20laser%20cutting%20and%20engraving%20services&key=YOUR_API_KEY
and got this result:
{
"html_attributions":[
],
"results":[
{
"formatted_address":"533 S Los Angeles St, Los Angeles, CA 90013, United States",
"geometry":{
"location":{
"lat":34.0454354,
"lng":-118.2487591
}
},
"icon":"https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id":"ef1923650a198ab42c3335c4f49d2ed52c9680fd",
"name":"CherryLaser laser cutting and engraving services",
"opening_hours":{
"open_now":true,
"weekday_text":[
]
},
"photos":[
{
"height":450,
"html_attributions":[
"CherryLaser laser cutting and engraving services"
],
"photo_reference":"CoQBcwAAAAUiQHaDoHqUqnWuOMdk9JywgVZJl7rb78dqIETdN0KktPnqkWv0XRsKl-UXqorIvWG3icTejJ_940XJSj0l3mrvhndafNrZCkbGvT4VMZGJ0pSdO00rTQCP07LdtYAuvDG19go2A6pYjTfka7dynxTAH1ehzEnz9Sdm8WDnJcjnEhA-qRmXzs9cnEHN3ZHzleR7GhRqQYi4c3z-BanOl369gjvCQp7hpw",
"width":1024
}
],
"place_id":"ChIJq8vSkzXGwoARS6Cc6F_FF5Q",
"reference":"CpQBhQAAAGwY3h9oSu4ec696UxnIJOcWqy16HXoukpXcjr0p70ifB8xDMWNHs6Bh1Frebr60YuxsjFvSqh_KIYTAGWfbpW1OZ-kVqG0VH8sfwd9Ga8ot9moKSm7z_1mWIqqkVq6ufs_BrfWGp-yDypp84xN07LdgxZ32p69bIQypgY2tXBtVohq8oxgp5IEtrS0Q23eX7RIQsSYPuSGdOqw6el5SJN6GihoU5-K2Ogh2ml2wEEPwnSCoW83hm_0",
"types":[
"point_of_interest","establishment"
]
}
],
"status":"OK"
}
So the place ID for your business is ChIJq8vSkzXGwoARS6Cc6F_FF5Q.
You can check details with Places API details request:
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJq8vSkzXGwoARS6Cc6F_FF5Q&key=YOUR_API_KEY
I am using a gmap autocomplete and sometimes the user doesn't hit any choice in the suggestion list. For example he types "Paris" in the input field and believes the search will be performed with Paris, however has the 'place_changed' of the gmap autcomplete was never called, the search cannot be perfomed.
How can I select by default the first choice of the suggestion list when the user doesn't make any choice ? I believe I could adapt the solution provided for the "enter issue" described here (Google maps Places API V3 autocomplete - select first option on enter) with a blur event handling, however this doesn't work.
Any hint ?
I think this is kind of defeating the purpose of the auto complete.
You could just retrieve the autocomplete predictions pro grammatically like so:
function initialize()
{
var service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({ input: 'some address' }, callback);
}
function callback(predictions, status)
{
if (status != google.maps.places.PlacesServiceStatus.OK)
{
alert(status);
return;
}
// Take the first result
var result = predictions[0]
// do as you wish with the result
}
Hope that helps