Yönlendirme özetini hesaplama

Yanıtın her bir yerindeki seyahat süresini ve mesafeyi hesaplamak için Metin Arama (Yeni) veya Yakınlardaki Arama (Yeni)'yı kullanmak için:

  1. İstekle birlikte RoutingParameters parametresini iletin. Bu parametreyle, yönlendirmenin başlangıç noktasının enlem ve boylam koordinatlarını belirtmek için setOrigin() kullanın. Bu parametre, yanıttaki her yerin süresini ve mesafesini hesaplamak için gereklidir.

  2. İstek nesnesini oluştururken .setRoutingSummariesIncluded(true) ekleyin.

Aşağıdaki istekte, Metin Arama (Yeni) yanıtındaki her bir yerin seyahat süresini ve mesafesini hesaplıyorsunuz:

// 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 sınıfı, bir arama isteğinden gelen yanıtı temsil eder. Yönlendirme özetleri listesini döndürmek için SearchByTextResponse.getRoutingSummaries() işlevini çağırabilirsiniz. SearchByTextResponse nesnesi şunları da içerir:

  • Eşleşen tüm yerleri temsil eden Place nesnelerinin listesi. Her eşleşen yer için bir Place nesnesi bulunur.
  • Her Place nesnesi yalnızca istekte iletilen alan listesi tarafından tanımlanan alanları içerir.

Bu örnekte, Yakındaki Arama yanıtındaki her yer için seyahat süresini ve mesafesini hesaplıyorsunuz. Bu örnekte, Avustralya'nın Sidney şehrindeki restoranlar aranıyor ve konum kısıtlaması ile yönlendirme başlangıcı aynı enlem ve boylam koordinatına ayarlanıyor:

// 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 sınıfı, bir arama isteğinden gelen yanıtı temsil eder. Yönlendirme özetleri listesini döndürmek için SearchNearbyResponse.getRoutingSummaries() işlevini çağırabilirsiniz. SearchNearbyResponse nesnesi şunları da içerir:

  • Eşleşen tüm yerleri temsil eden Place nesnelerinin listesi. Eşleşen her yer için bir Place nesnesi bulunur.
  • Her Place nesnesi yalnızca istekte iletilen alan listesi tarafından tanımlanan alanları içerir.

Konum kısıtlaması ve yönlendirme kaynağı için aynı koordinatları kullanmanız gerekmez. Örneğin, arama sonuçlarını bu daireyle sınırlamak için konum kısıtlamasını Sidney'in merkez noktası olarak ayarlarsınız. Ancak daha sonra yönlendirme başlangıcını evinizin koordinatlarına, yani arama çemberi içinde farklı bir konuma ayarlarsınız. Ardından istek, arama sonuçlarını daireyle kısıtlar ve rota özetlerini evinizin konumuna göre hesaplar.

Seyahat seçeneklerini belirleme

Varsayılan olarak süre ve mesafe hesaplamaları araba için yapılır. Ancak, arama sonuçlarında araç türünü ve diğer seçenekleri kontrol edebilirsiniz.

  • Ulaşım modunu DRIVE, BICYCLE, WALK veya TWO_WHEELER olarak ayarlamak için routingParameters.setTravelMode() simgesini kullanın. Bu seçenekler hakkında daha fazla bilgi için Rotalar için kullanılabilen araç türleri başlıklı makaleyi inceleyin.

  • Yönlendirme tercihi seçeneğini TRAFFIC_UNAWARE (varsayılan), TRAFFIC_AWARE veya TRAFFIC_AWARE_OPTIMAL olarak ayarlamak için routingParameters.setRoutingPreference() simgesini kullanın. Her seçeneğin veri kalitesi ve gecikme düzeyleri farklıdır. Daha fazla bilgi için Trafik verilerinin nasıl ve dahil edilip edilmeyeceğini belirtme başlıklı makaleyi inceleyin.
  • routingParameters.setRouteModifiers() kullanarak avoidTolls, avoidHighways, avoidFerries ve avoidIndoor değerlerini belirtin. Bu seçenekler hakkında daha fazla bilgi için Kaçınılacak rota özelliklerini belirtme başlıklı makaleyi inceleyin.

Sonraki örnekte, ulaşım şeklini DRIVE olarak belirtiyorsunuz ve otoyollardan kaçınmak istiyorsunuz:

// 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();
    });