I'm having an issue with the new Google Autocomplete API not returning street addresses as in earlier versions:
Looking at Google's documentation, there's a property called includedPrimaryTypes which accepts values like "street_address" or "street_number", but this doesn't seem to be working correctly it returns only specific street addresses not all that Google offers.
My Current Code
const { AutocompleteSuggestion } = await google.maps.importLibrary("places");
const { suggestions: fetchedSuggestions } = await AutocompleteSuggestion.fetchAutocompleteSuggestions({
input,
sessionToken: token.value,
// includedPrimaryTypes: ["street_address", "street_number", "premise"],
});
suggestions.value = await Promise.all(
fetchedSuggestions.map(
async (suggestion: google.maps.places.AutocompleteSuggestion) => {
const place = suggestion.placePrediction.toPlace();
await place.fetchFields({
fields: ["formattedAddress"],
});
return {
placePrediction: suggestion.placePrediction,
formattedAddress: place.formattedAddress,
};
},
),
);
The "address"
filter in the legacy Place Autocomplete is indeed not available in the new Place Autocomplete; yet the same functionality can be achieved with additional flexibility.
Previous Behavior: The API accepted a type: ["address"] parameter which returned only full addresses
Results would include only street-level results, but not necessarily all street-level results: you'd be missing results of type premise
(buildings).
To get the same results, or rather more and better results, use this:
includedPrimaryTypes: ["premise", "subpremise", "street_address", "route"],
Note: "street_number"
will not get you any results; nothing has that as its primary type.
Depending on your needs, you may want to remove "route"
from the list, if you want to be sure that the subsequent Place Details request will come back with street_number
(address component) in the response.
Otherwise, when including "route"
in includedPrimaryTypes
, you may (likely will) often run into situations where the Autocomplete prediction actually represents a street or road (or section of one) and not a building.
For more details, see https://issuetracker.google.com/115569354
Place Details missing street_number; Autocomplete result for a street address that cannot be interpolated on Google Maps will represent only (a section of) the road, not the individual building; street_number is available in the terms field in Autocomplete result.
If that sounds like it would be problematic in some cases, you can avoid it by removing "route"
:
includedPrimaryTypes: ["premise", "subpremise", "street_address"],
Additionally, since includedPrimaryTypes
supports up to 5 values, and in some regions street-level addresses either don't exist or are more commonly represented by high-precision (single-building) postal codes, you may want to add "plus_code"
and/or "postal_code"
.