このページでは、Place クラス(新規)と PlacesService(以前のクラス)で使用される近隣検索の違いについて説明し、比較用のコード スニペットをいくつか紹介します。
- 以前の PlacesServiceにはnearbySearch()メソッドがあり、指定された範囲内で、キーワードやタイプによる場所の検索が可能です。
- Placeクラスには- searchNearby()メソッドがあり、場所データ フィールドと場所タイプの選択肢を拡大して柔軟性を高め、場所の種類で指定されたエリア内の場所を検索できます。
次の表に、Place クラスと PlacesService の近隣検索メソッドの主な違いをいくつか示します。
| PlacesService(レガシー) | Place(新規) | 
|---|---|
| nearbySearch() | searchNearby() | 
| PlaceSearchRequest | SearchNearbyRequest | 
| 結果オブジェクトと google.maps.places.PlacesServiceStatusレスポンスを処理するためにコールバックを使用する必要があります。 | Promise を使用し、非同期で動作します。 | 
| PlacesServiceStatusチェックが必要です。 | ステータス チェックは不要で、標準のエラー処理を使用できます。詳細 | 
| 位置情報のバイアスのみをサポートします。 | 位置情報のバイアスと位置情報の制限をサポートします。 | 
| 利用可能なすべてのデータ フィールド(サポートされるフィールドの一部)を返します。特定のフィールドに制限することはできません。 | リクエストされた場所データ フィールドのみを返します。 Placeクラスは、フィールドの選択肢を拡大し、定期的に更新します。 | 
| 場所タイプの固定セットに限定されます。 | 場所タイプの選択肢が拡大され、定期的に更新されます。 | 
| キーワードを使用したテキストベースの検索をサポート。 | テキストベースの検索はサポートされていません。代わりにテキスト検索(新版)を使用してください。 | 
コードの比較
このセクションでは、近隣検索メソッドのコードを比較して、Places Service と Place クラスの違いを示します。コード スニペットは、テキストベースの検索リクエストを行うために各 API で必要なコードを示しています。
Nearby Search(従来版)
以前の Nearby Search では、指定された範囲内で、キーワードやタイプによる場所の検索が可能です。場所データ フィールドを使用して検索を制限することはできません。そのため、利用可能なすべてのフィールドが各リクエストとともに返されます。次のスニペットは、nearbySearch() を呼び出してオーストラリアのシドニーにあるレストランに関する情報を返す方法を示しています。リクエストは同期型で、コールバックを使用し、PlacesServiceStatus の条件付きチェックを含みます。
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,
  });
}
その他の情報
Nearby Search(新規)
新しいバージョンの Nearby Search では、以前のバージョンから次のような点が改善されています。
- 返す場所データ フィールドを指定する機能。
- 非同期オペレーションを可能にする Promise の使用。
- PlacesServiceのステータスを確認する必要はありません。代わりに標準のエラー処理を使用できます。
次のコード スニペットは、レストランの Nearby Search リクエストを行う関数を示しています。この例では、rankPreference オプションを使用して、検索結果を人気度でランク付けする方法を示します(前のバージョンでは、rankBy オプションを使用してランキングを指定しています)。searchNearby() メソッドは await 演算子を使用するため、async 関数内でのみ使用できます。
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");
  }
}