googlemaps / js-api-loader

Load the Google Maps JavaScript API script dynamically.
Apache License 2.0
347 stars 64 forks source link

Port this over to Places API (New)? #841

Open BenJackGill opened 6 months ago

BenJackGill commented 6 months ago

Is your feature request related to a problem? Please describe. I am looking for a npm package to use with Places API (New) (places.googleapis.com) as opposed to the original Places API (places-backend.googleapis.com) which this package currently uses. I was hoping this one could be ported/copied/modified to work with the Places API (New).

Describe the solution you'd like New npm package for Places API (New) (places.googleapis.com) to be used on client browsers. Or change this one so it supports the new API.

Describe alternatives you've considered This package, but it does not work with the new API.

Additional context None

wangela commented 6 months ago

If you would like to upvote the priority of this issue, please comment below or react on the original post above with :+1: so we can see what is popular when we triage.

@BenJackGill Thank you for opening this issue. 🙏 Please check out these other resources that might help you get to a resolution in the meantime:

This is an automated message, feel free to ignore.

usefulthink commented 6 months ago

@BenJackGill this package doesn't really interact with the Places API at all, it only loads the Google Maps JavaScript API in the browser. I don't think there is a way to choose the version of the places library that is used by the Maps API (i.e., when loading it via google.maps.importLibrary('places')).

BenJackGill commented 6 months ago

I think it does interact with the Places API when it loads the libraries.

I am using this package for Places autocomplete, and I restrict my API credentials to only the APIs that need access (principle of least privilege).

In doing so, for this package to work it requires access to both the Maps JavaScript API (maps-backend.googleapis.com) and Places API (places-backend.googleapis.com). Without access to the Places API it throws an error saying This API key is not authorized to use this service or API. Places API error: ApiTargetBlockedMapError which suggest thats this package is using the Places API somewhere.

Here is my code that uses @googlemaps/js-api-loader to load the Places library:

import { Loader } from "@googlemaps/js-api-loader";

  const loader = new Loader({
    apiKey: process.env.GCP_PLACES_API_KEY || "",
    version: "weekly",
    libraries: ["places"], // <--- Maybe this is where it uses Places API?
  });
  await loader.load();
  autocompleteService = new google.maps.places.AutocompleteService();
  sessionToken.value = new google.maps.places.AutocompleteSessionToken();

// More stuff here for autocomplete predictions

I tried migrating from the old Places API to the new one by changing the API credential restriction from places-backend.googleapis.com to places.googleapis.com and it started throwing the same error of This API key is not authorized to use this service or API. Places API error: ApiTargetBlockedMapError which suggests that the package only works with original places-backend.googleapis.com and not the new places.googleapis.com version.

So my feature request was hoping to either get the new places.googleapis.com version working here or maybe a standalone package if that is a better direction.

Maybe I am asking this in the wrong place?

usefulthink commented 6 months ago

Ah, I see. Indeed, the 'places' library dependency of the maps API is still mostly using the older version of the places API. As far as I know, only the new Place class and other new parts of the places library are using the newer version of the API.

I think these new classes are meant to supercede and eventually replace the PlacesService and related classes, but I tend to believe the two versions are going to coexist for quite a while.

wangela commented 5 months ago

@BenJackGill Indeed, the line libraries: ["places]" loads the Places library which provides access to both the old and the new Places APIs.

See the Get Started with Place class page to make sure you've enabled the Places API (New) on the project associated with your API key and you've added the new API to your list of restrictions applied to that key.

If you're looking for the new Place Autocomplete you can use the widget provided as an HTML element here: https://developers.google.com/maps/documentation/javascript/place-autocomplete-new

tiefenb commented 4 months ago

We use also the Places API for autocomplete with this library. We got contacted by Google who asked if we could move to the new Places API (new). Now we are waiting of the support by this library.

usefulthink commented 4 months ago

If you want to move to Places API (new), you can just start by using the new Place class as mentioned by @wangela above. Nothing in way you're loading the Maps API needs to be changed for this.

BenJackGill commented 3 months ago

I had a second crack at this today. What I found is you need 3 APIs turned on, they are Places API, Places API (new), and Maps JavaScript API. I think my original confusion was that I acidentally turned off Places API thinking it wasn't needed with Places API (new). But that is wrong. All are required together as documented here.

Having said that I still couldn't complete the conversion because there are many missing TypeScript types for Places API (new) which made navigating the conversion difficult. I advise sticking with the original Places API for now if you can. Places API (new) is not fully baked yet.

usefulthink commented 3 months ago

That's a good point. Can you point out the types that are missing or maybe open an issue in the repo for the @types/google.maps package?