คำนวณสรุปการกำหนดเส้นทาง
วิธีใช้การค้นหาด้วยข้อความ (ใหม่) หรือการค้นหาใกล้เคียง (ใหม่) เพื่อคำนวณระยะเวลาเดินทางและระยะทางไปยังแต่ละสถานที่ในคำตอบ
-
ส่งพารามิเตอร์
RoutingParameters
ในคำขอโดยใช้setOrigin()
เพื่อระบุพิกัดละติจูดและลองจิจูดของต้นทางการกำหนดเส้นทาง ต้องระบุพารามิเตอร์นี้เพื่อคำนวณระยะเวลาและระยะทางไปยังสถานที่แต่ละแห่งใน การตอบกลับ -
เมื่อสร้างออบเจ็กต์คำขอ ให้เพิ่ม
.setRoutingSummariesIncluded(true)
ใช้การค้นหาข้อความ (ใหม่)
ในคำขอต่อไปนี้ คุณจะคำนวณระยะเวลาการเดินทางและระยะทางไปยังสถานที่แต่ละแห่งในคำตอบของ Text Search (ใหม่) ได้
// 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
ที่แสดงสถานที่ที่ตรงกันทั้งหมด โดยมีออบเจ็กต์Place
1 รายการต่อสถานที่ที่ตรงกัน - ออบเจ็กต์
Place
แต่ละรายการจะมีเฉพาะฟิลด์ที่กำหนดโดยรายการฟิลด์ที่ส่งในคำขอเท่านั้น
ใช้การค้นหาในบริเวณใกล้เคียง
ในตัวอย่างนี้ คุณจะคำนวณระยะเวลาในการเดินทางและระยะทางไปยังแต่ละสถานที่ใน การตอบกลับการค้นหาใกล้เคียง ตัวอย่างนี้จะค้นหาร้านอาหารในซิดนีย์ ออสเตรเลีย และตั้งค่าข้อจำกัดด้านสถานที่ตั้งและต้นทางการกำหนดเส้นทางเป็นพิกัดละติจูดและลองจิจูดเดียวกัน
// 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
ที่แสดงสถานที่ที่ตรงกันทั้งหมด โดยมีออบเจ็กต์Place
1 รายการต่อสถานที่ที่ตรงกัน 1 แห่ง - ออบเจ็กต์
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(); });