คำนวณข้อมูลสรุปการกำหนดเส้นทาง
วิธีใช้ การค้นหาข้อความ (ใหม่) หรือ การค้นหาในบริเวณใกล้เคียง (ใหม่) เพื่อคำนวณระยะเวลาและระยะทางในการเดินทางไปยังสถานที่แต่ละแห่งในการตอบกลับ:
-
ส่งพารามิเตอร์
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ที่แสดงถึงสถานที่ที่ตรงกันทั้งหมด โดยมีออบเจ็กต์Place1 รายการต่อสถานที่ที่ตรงกัน - ออบเจ็กต์
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ที่แสดงถึงสถานที่ที่ตรงกันทั้งหมด โดยมีออบเจ็กต์Place1 รายการต่อสถานที่ที่ตรงกัน - ออบเจ็กต์
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(); });