Google Maps Places Autocomplete missing geometry

2020-03-21 10:35发布

问题:

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?

回答1:

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



回答2:

Please be sure you set 'geometry' field in autocomplete like this:

autocomplete.setFields(['address_component', 'geometry']);



回答3:

Looks like it has been fixed by Google :)