<br />

**European Economic Area (EEA) developers**

> [!NOTE]
> If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](https://developers.google.com/maps/comms/eea/faq.md.txt).

This page explains the differences between nearby search as used in the
[`Place`](https://developers.google.com/maps/documentation/javascript/reference/place.md.txt) class (new) and the
[`PlacesService`](https://developers.google.com/maps/documentation/javascript/reference/places-service.md.txt)
(legacy), and provides some code snippets for comparison.

- The legacy `PlacesService` has a `nearbySearch()` method, which lets you search for places within a specified area by keyword or type.
- The `Place` class has a `searchNearby()` method which lets you search for places within a specified area by place type, utilizing an expanded selection of place data fields and place types for greater flexibility.

The following table lists some of the main differences in nearby search methods
between the `Place` class and `PlacesService`:

| [`PlacesService`](https://developers.google.com/maps/documentation/javascript/reference/places-service.md.txt) (Legacy) | [`Place`](https://developers.google.com/maps/documentation/javascript/reference/place.md.txt) (New) |
|---|---|
| [`nearbySearch()`](https://developers.google.com/maps/documentation/javascript/reference/places-service.md.txt#PlacesService.nearbySearch) | [`searchNearby()`](https://developers.google.com/maps/documentation/javascript/reference/place.md.txt#Place.searchNearby) |
| [`PlaceSearchRequest`](https://developers.google.com/maps/documentation/javascript/reference/places-service.md.txt#PlaceSearchRequest) | [`SearchNearbyRequest`](https://developers.google.com/maps/documentation/javascript/reference/place.md.txt#SearchNearbyRequest) |
| Requires the use of a callback to handle the results object and `google.maps.places.PlacesServiceStatus` response. | Uses Promises, and works asynchronously. |
| Requires a `PlacesServiceStatus` check. | No required status check, can use standard error handling. [Learn more](https://developers.google.com/maps/documentation/javascript/reference/errors.md.txt). |
| Supports only location bias. | Supports location bias and location restriction. |
| Returns all available data fields (a [subset of the supported fields](https://developers.google.com/maps/documentation/places/web-service/place-data-fields#places-api-fields-support)); cannot be constrained to specific fields. | Returns only the requested [place data fields](https://developers.google.com/maps/documentation/javascript/place-class-data-fields.md.txt); the `Place` class offers an expanded and regularly updated selection of fields. |
| Limited to a fixed set of [place types](https://developers.google.com/maps/documentation/javascript/supported_types). | Access an expanded and regularly updated selection of [place types](https://developers.google.com/maps/documentation/javascript/place-types.md.txt). |
| Supported text-based search with the [keyword](https://developers.google.com/maps/documentation/javascript/reference/places-service.md.txt#PlaceSearchRequest.keyword). | Text-based search is not supported, use [Text Search (New)](https://developers.google.com/maps/documentation/javascript/places-migration-search#place-class-new) instead. |

## Code comparison

This section compares code for nearby search methods to illustrate the
differences between the Places Service and the
Place class. The code snippets show the code
required on each respective API to make a text-based search request.

### Nearby Search (Legacy)

The legacy Nearby Search lets you search for places within a specified area by
keyword or type. There is no way to constrain searches by using place data
fields, so that all of the available fields are returned with each request.
The following snippet shows calling `nearbySearch()` to return information about
restaurants in Sydney, Australia. The request is synchronous, uses a callback,
and includes a required conditional check on `PlacesServiceStatus`.

```javascript
let map;
let service;

function initMap() {
  const sydney = new google.maps.LatLng(-33.867, 151.195);

  map = new google.maps.Map(document.getElementById("map"), {
    center: sydney,
    zoom: 15,
  });

  const request = {
    location: sydney,
    radius: '500',
    type: ['restaurant']
  };

  service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

// Helper function to create markers.
function createMarker(place) {
  if (!place.geometry || !place.geometry.location) return;

  const marker = new google.maps.Marker({
    map,
    position: place.geometry.location,
    title: place.name,
  });
}
```

#### Learn more

- [See the documentation for legacy Nearby Search](https://developers.google.com/maps/documentation/javascript/places#place_search_requests)
- [See the `searchNearby()` reference](https://developers.google.com/maps/documentation/javascript/reference/places-service.md.txt#PlacesService.nearbySearch)

### Nearby Search (New)

The new version of Nearby Search improves upon its predecessor in the following
ways:

- The ability to specify which place data fields to return.
- The use of Promises which enables asynchronous operation.
- No need to check for the status of `PlacesService`; standard error handling can be used instead.

The following code snippet shows a function which makes a Nearby Search request
for restaurants. This example shows using the `rankPreference` option to rank
search results by popularity (in the previous version ranking is specified
using the `rankBy` option). Because the `searchNearby()` method uses the `await`
operator it can only be used inside an `async` function.

```javascript
async function nearbySearch() {
  // Restrict within the map viewport.
  let center = new google.maps.LatLng(52.369358, 4.889258);
  const request = {
    // Required parameters.
    fields: ["displayName", "location", "businessStatus"],
    locationRestriction: {
      center: center,
      radius: 500,
    },
    // Optional parameters.
    includedPrimaryTypes: ["restaurant"],
    maxResultCount: 5,
    rankPreference: google.maps.places.SearchNearbyRankPreference.POPULARITY,
    language: "en-US",
    region: "us",
  };

  const { places } = await google.maps.places.Place.searchNearby(request);

  if (places.length) {
    console.log(places);

    // Create a new bounds, which will be extended with each result.
    const bounds = new google.maps.LatLngBounds();

    // Loop through and get all the results.
    places.forEach((place) => {
      const markerView = new google.maps.marker.AdvancedMarkerElement({
        map,
        position: place.location,
        title: place.displayName,
      });

      bounds.extend(place.location);
      console.log(place);
    });
    map.fitBounds(bounds);
  } else {
    console.log("No results");
  }
}
```

#### Learn more

- [See the complete example code](https://developers.google.com/maps/documentation/javascript/examples/place-nearby-search.md.txt)
- [See the documentation for Nearby Search (New)](https://developers.google.com/maps/documentation/javascript/nearby-search.md.txt)
- [See the `searchNearby()` reference](https://developers.google.com/maps/documentation/javascript/reference/place.md.txt#Place.searchNearby)