'Google Maps Places Autocomplete missing geometry
The Google Maps Places Autocomplete does not work as expected anymore.
https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete
When inserting a search string in the map (for example "ZKM"), it will give some recommendations in the drop down list. In this case it will be "ZKM | Zentrum für Kunst und Medientechnologie Karlsruhe, Lorenzstraße, Karlsruhe, Deutschland". When clicking this item, no marker will be placed on the map!
But when searching for "Karlsruhe" and clicking the first search result "Karlsruhe, Deutschland" the marker is placed correctly.
In the code a function called getPlace() ist called.
var place = autocomplete.getPlace();
The place should contain an object "geometry", but it does not.
Is there any workaround for this problem?
Solution 1:[1]
Please be sure you set 'geometry' field in autocomplete like this:
autocomplete.setFields(['address_component', 'geometry']);
Solution 2:[2]
Take a look at the beginning of the function:
if (!place.geometry) {
return;
}
A PlaceResult may, but must not have a geometry-property.
To clarify how the autocomplete works:
when you type something, the API will request the predictions(to populate the dropdown). Only when you select a place from the dropdown it requests the data(including the geometry) for the particular place.
Obviously there are inconsistencies of the used data(the API shouldn't suggest a place where no informations are accessible), but that's how it is, it may happen that you get a prediction without a place.
Workaround: AFAIK no
Solution 3:[3]
Yes, the place contains an object "geometry".
To find this, you should use this :
autocomplete.getPlace().geometry.location.lat() //for the latitude
autocomplete.getPlace().geometry.location.lng() //for the longitude
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Maxim Georgievskiy |
| Solution 2 | Dr.Molle |
| Solution 3 | Aupire |
