To use the Google Places API on a web page, create a Google Cloud project with billing enabled, turn on the Places API (New), create a restricted API key, load the Maps JavaScript API, and call Place.searchByText() from the places library. The older PlacesService and findPlaceFromQuery() calls still appear in many tutorials, but they are legacy and Google directs new projects to the Place class.
This guide builds a working page that searches for places, drops markers on a map, and shows a name in an info window. It also shows the equivalent server-side REST call.
https://example.com/*) and limit it to the APIs you use. For a server key, restrict by IP address instead.A browser key is visible to anyone who opens the page source, so the restrictions are what protect your quota. Never use an unrestricted key in client code, and never put a server key in front-end files.
Use the async loading pattern. The callback runs after the loader is ready, and importLibrary() fetches only the libraries you need.
The inline snippet is Google's standard dynamic library loader. If you prefer something shorter, a plain <script async src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&loading=async&callback=initMap"></script> tag also works, and you then call importLibrary() inside initMap.
Create places.js. It imports the libraries, runs a text search, and adds one marker per result.
What this does:
fields tells the API which data to return. You are billed according to the fields you request, so ask only for what you display.AdvancedMarkerElement replaces the deprecated google.maps.Marker. It needs a mapId, which you can create in the Cloud Console.fitBounds() zooms the map so every result is visible.await works because the script is loaded with type="module".Other useful calls on the same class include Place.searchNearby() for a circle around a point, new Place({ id }).fetchFields({ fields: [...] }) to load details for a known place ID, and the PlaceAutocompleteElement web component for a search box with suggestions.
For back-end code, call the Places API (New) over HTTPS. Every request needs a field mask, sent in the X-Goog-FieldMask header.
Note that in the REST response displayName is an object with a text field, while in the JavaScript Place class it is already a string.
| Topic | Legacy (PlacesService) | New (Place class, REST v1) |
|---|---|---|
| Status | Legacy; not available to new projects | Current recommended API |
| Search | findPlaceFromQuery, textSearch, callbacks | Place.searchByText, promises |
| Field control | fields array on some calls | Required field list or field mask on every call |
| REST host | maps.googleapis.com/maps/api/place/... | places.googleapis.com/v1/... |
| Markers | google.maps.Marker (deprecated) | AdvancedMarkerElement |
If you maintain old code, plan to migrate, because legacy services get no new features.
Usually the Places API (New) is not enabled, billing is off, or the key's API or referrer restrictions exclude the request. Check the browser console for the exact message.
There is a monthly free usage allowance that depends on the product SKU and your plan, but billing must be enabled and usage beyond the allowance is charged. Request only the fields you need to keep costs down.
Places policies require attribution, and some uses of the data are restricted. Read the current terms before you cache or display results outside a Google map.
Use the PlaceAutocompleteElement from the places library, or the Autocomplete (New) REST endpoint, and then fetch details with the chosen place ID.
Enable the Places API (New), restrict your key, load the Maps JavaScript API with importLibrary(), and use Place.searchByText() with an explicit fields list. Show results with AdvancedMarkerElement, and use the v1 REST endpoint with a field mask from servers. Treat PlacesService and google.maps.Marker as legacy code to migrate.