ルーティングの概要を計算する
テキスト検索(新版)または周辺検索(新版)を使用して、レスポンス内の各場所までの移動時間と距離を計算するには:
-
リクエストで
RoutingParameters
パラメータを渡し、setOrigin()
を使用してルートの出発地の緯度と経度の座標を指定します。このパラメータは、レスポンス内の各場所までの所要時間と距離を計算するために必要です。 -
リクエスト オブジェクトをビルドするときに、
.setRoutingSummariesIncluded(true)
を追加します。
テキスト検索(新版)を使用する
次のリクエストでは、テキスト検索(新版)のレスポンスに含まれる各場所までの移動時間と距離を計算します。
// Specify the list of fields to return. final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME); // Define the routing parameters object and pass the routing origin. RoutingParameters routingParameters = RoutingParameters.builder() .setOrigin(toLatLng("-33.8688, 151.1957362")) .build(); // Use the builder to create a SearchByTextRequest object and pass the routing parameters. // Set setRoutingSummariesIncluded to true. final SearchByTextRequest searchByTextRequest = SearchByTextRequest.builder("Spicy Vegetarian Food in Sydney, Australia", placeFields) .setMaxResultCount(10) .setRoutingParameters(routingParameters) .setRoutingSummariesIncluded(true) .build(); // Call PlacesClient.searchByText() to perform the search. // Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg objects. placesClient.searchByText(searchByTextRequest) .addOnSuccessListener(response -> { List<Place> places = response.getPlaces(); List<RoutingSummary> routingSummaries = response.getRoutingSummaries(); List<Leg> legs = routingSummaries.get(0).getLegs(); Duration duration = legs.get(0).getDuration(); });
SearchByTextResponse
クラスは、検索リクエストからのレスポンスを表します。SearchByTextResponse.getRoutingSummaries()
を呼び出して、ルーティングの概要のリストを返すことができます。SearchByTextResponse
オブジェクトには次の要素も含まれます。
- 一致するすべての場所を表す
Place
オブジェクトのリスト。一致する場所ごとに 1 つのPlace
オブジェクトがあります。 - 各
Place
オブジェクトには、リクエストで渡されたフィールド リストで定義されたフィールドのみが含まれます。
Nearby Search を使用する
この例では、Nearby Search レスポンス内の各場所までの移動時間と距離を計算します。この例では、オーストラリアのシドニーのレストランを検索し、位置制限とルートの出発地を同じ緯度と経度の座標に設定します。
// Specify the list of fields to return. final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME); // Define the search area as a 500 meter diameter circle in Sydney, Australia. LatLng center = new LatLng(-33.8688, 151.1957362); CircularBounds circle = CircularBounds.newInstance(center, /* radius = */ 500); // Define the routing parameters object and pass the routing origin. RoutingParameters routingParameters = RoutingParameters.builder() .setOrigin(toLatLng("-33.8688, 151.1957362")) .build(); // Use the builder to create a SearchNearbyRequest object and pass the routing parameters. // Set setRoutingSummariesIncluded to true. final SearchNearbyRequest searchNearbyRequest = SearchNearbyRequest.builder(/* location restriction = */ circle, placeFields) .setIncludedTypes(includedTypes) .setMaxResultCount(10) .setRoutingParameters(routingParameters) .setRoutingSummariesIncluded(true) .build(); // Call PlacesClient.searchNearby() to perform the search. // Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg objects. placesClient.searchNearby(searchNearbyRequest) .addOnSuccessListener(response -> { List<Place> places = response.getPlaces(); List<RoutingSummary> routingSummaries = response.getRoutingSummaries(); List<Leg> legs = routingSummaries.get(0).getLegs(); Duration duration = legs.get(0).getDuration(); });
SearchNearbyResponse
クラスは、検索リクエストからのレスポンスを表します。SearchNearbyResponse.getRoutingSummaries()
を呼び出して、ルーティングの概要のリストを返すことができます。SearchNearbyResponse
オブジェクトには次の要素も含まれます。
- 一致するすべての場所を表す
Place
オブジェクトのリスト。一致する場所ごとに 1 つのPlace
オブジェクトがあります。 - 各
Place
オブジェクトには、リクエストで渡されたフィールド リストで定義されたフィールドのみが含まれます。
位置情報の制限とルートの出発地で同じ座標を使用する必要はありません。たとえば、シドニーの中心点を位置制限に設定して、検索結果をその円内に制限します。しかし、その後、ルーティングの出発地を家の座標(検索円内の別の場所)に設定します。リクエストは、検索結果を円内に制限し、家の位置に基づいてルートの概要を計算します。
移動手段を指定する
デフォルトでは、所要時間と距離の計算は車を対象としています。ただし、検索では車両タイプやその他のオプションを制御できます。
-
routingParameters.setTravelMode()
を使用して、交通手段のモードをDRIVE
、BICYCLE
、WALK
、TWO_WHEELER
のいずれかに設定します。これらのオプションの詳細については、ルートで利用可能な車両タイプをご覧ください。 -
routingParameters.setRoutingPreference()
を使用して、ルーティング設定オプションをTRAFFIC_UNAWARE
(デフォルト)、TRAFFIC_AWARE
、TRAFFIC_AWARE_OPTIMAL
に設定します。各オプションには、さまざまなレベルのデータ品質とレイテンシがあります。詳細については、トラフィック データを含める方法と含めるかどうかを指定するをご覧ください。 -
routingParameters.setRouteModifiers()
を使用して、avoidTolls
、avoidHighways
、avoidFerries
、avoidIndoor
を指定します。これらのオプションの詳細については、回避するルートの特徴を指定するをご覧ください。
次の例では、移動手段を DRIVE
に指定し、高速道路を避けるように指定しています。
// Specify the list of fields to return. final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME); // Define the routing modifiers object. RouteModifiers routeModifiers = RouteModifiers.builder() .setAvoidHighways(true) .build(); // Define the routing parameters object and pass the routing origin. RoutingParameters routingParameters = RoutingParameters.builder() .setOrigin(toLatLng("-33.8688, 151.1957362")) .setTravelMode(DRIVE) .setRouteModifiers(routeModifiers) .build(); // Use the builder to create a SearchByTextRequest object and pass the routing parameters. // Set setRoutingSummariesIncluded to true. final SearchByTextRequest searchByTextRequest = SearchByTextRequest.builder("Spicy Vegetarian Food in Sydney, Australia", placeFields) .setMaxResultCount(10) .setRoutingParameters(routingParameters) .setRoutingSummariesIncluded(true) .build(); // Call PlacesClient.searchByText() to perform the search. // Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg objects. placesClient.searchByText(searchByTextRequest) .addOnSuccessListener(response -> { List<Place> places = response.getPlaces(); List<RoutingSummary> routingSummaries = result.getRoutingSummaries(); List<Leg> legs = routingSummaries.get(0).getLegs(); Duration duration = legs.get(0).getDuration(); });