ดูข้อมูลเส้นทาง

ทำตามคู่มือนี้เพื่อตั้งค่าแอปให้ดึงข้อมูลเวลา ระยะทาง และช่วงของเส้นทางสำหรับเส้นทางปัจจุบัน

ภาพรวม

หากต้องการรับข้อมูลเกี่ยวกับเส้นทางปัจจุบัน ให้รับพร็อพเพอร์ตี้ที่เหมาะสมจากอินสแตนซ์ navigator ดังนี้

  • GMSNavigator.timeToNextDestination เพื่อรับเวลาที่คาดการณ์ไว้ว่าจะถึงจุดหมายปลายทางถัดไปในเส้นทางปัจจุบันเป็นวินาที
  • GMSNavigator.distanceToNextDestination เพื่อรับระยะทางไปยังจุดหมายปลายทางถัดไปในเส้นทางปัจจุบันเป็นเมตร
  • GMSNavigationDelayCategory เพื่อรับหมวดหมู่การหน่วงเวลาสำหรับการจราจร
  • GMSNavigator.currentRouteLeg เพื่อรับข้อมูลเกี่ยวกับช่วงของเส้นทางปัจจุบัน
  • GMSNavigator.traveledPath เพื่อรับเส้นทางที่เดินทางล่าสุด

ดูโค้ด

การรับเวลาที่จะถึงจุดหมายปลายทางถัดไป

หากต้องการรับเวลาที่จะถึงจุดหมายปลายทางถัดไป ให้เรียกใช้ timeToNextDestination() ซึ่งจะแสดงผลค่า NSTimeInterval ตัวอย่างต่อไปนี้แสดงการบันทึกเวลาที่จะถึงจุดหมายปลายทางถัดไป

Swift

if let navigator = mapView.navigator {
  let time = navigator.timeToNextDestination
  let minutes = floor(time/60)
  let seconds = round(time - minutes * 60)
  NSLog("Time to next destination: %.0f:%.0f", minutes, seconds)
}

Objective-C

NSTimeInterval time = _mapView.navigator.timeToNextDestination;
int minutes = floor(time/60);
int seconds = round(time - minutes * 60);
NSLog(@"%@", [NSString stringWithFormat:@"Time to next destination: %i:%i.", minutes, seconds]);

การรับระยะทางที่จะถึงจุดหมายปลายทางถัดไป

หากต้องการรับระยะทางที่จะถึงจุดหมายปลายทางถัดไป ให้เรียกใช้ distanceToNextDestination() ซึ่งจะแสดงผลค่า CLLocationDistance โดยระบุหน่วยเป็นเมตร

Swift

if let navigator = mapView.navigator {
  let distance = navigator.distanceToNextDestination
  let miles = distance * 0.00062137
  NSLog("Distance to next destination: %.2f miles.", miles)
}

Objective-C

CLLocationDistance distance = _mapView.navigator.distanceToNextDestination;
double miles = distance * 0.00062137;
NSLog(@"%@", [NSString stringWithFormat:@"Distance to next destination: %.2f.", miles]);

การรับสภาพการจราจรที่จะถึงจุดหมายปลายทางถัดไป

หากต้องการรับค่าที่ระบุการจราจรที่จะถึงจุดหมายปลายทางถัดไป ให้เรียกใช้ delayCategoryToNextDestination พารามิเตอร์นี้จะแสดงผล GMSNavigationDelayCategory ตัวอย่างต่อไปนี้แสดงการประเมินผลลัพธ์และการบันทึกข้อความการจราจร

Swift

if let navigator = mapView.navigator {
  // insert sample for evaluating traffic value
  let delay = navigator.delayCategoryToNextDestination
  let traffic = "unavailable"
  switch delay {
    case .noData:
      traffic = "unavailable"
    case .heavy:
      traffic = "heavy"
    case .medium:
      traffic = "moderate"
    case .light:
      traffic = "light"
    default:
      traffic = "unavailable"
  }
  print("Traffic is \(traffic).")
}

Objective-C

GMSNavigationDelayCategory delay = mapView.navigator.delayCategoryToNextDestination;
NSString *traffic = @"";

switch (delayCategory) {
    case GMSNavigationDelayCategoryNoData:
      traffic = @"No Data";
      break;
    case GMSNavigationDelayCategoryHeavy:
      traffic = @"Heavy";
      break;
    case GMSNavigationDelayCategoryMedium:
      traffic = @"Medium";
      break;
    case GMSNavigationDelayCategoryLight:
      traffic = @"Light";
      break;
    default:
      NSLog(@"Invalid delay category: %zd", delayCategory);
 }

NSLog(@"%@", [NSString stringWithFormat:@"Traffic is %@.", traffic]);

การรับข้อมูลเกี่ยวกับช่วงปัจจุบัน

หากต้องการรับข้อมูลเกี่ยวกับช่วงของเส้นทางปัจจุบัน ให้เรียกใช้ currentRouteLeg ซึ่งจะแสดงผลอินสแตนซ์ GMSRouteLeg ที่คุณสามารถรับข้อมูลต่อไปนี้ได้

  • จุดหมายปลายทางของช่วง
  • พิกัดสุดท้ายในช่วง
  • เส้นทางที่มีพิกัดซึ่งประกอบขึ้นเป็นช่วงของเส้นทาง

ตัวอย่างต่อไปนี้แสดงการบันทึกชื่อและพิกัดละติจูด/ลองจิจูดสำหรับช่วงของเส้นทางถัดไป

Swift

if let navigator = mapView.navigator {
  let currentLeg = navigator.currentRouteLeg
  let nextDestination = currentLeg?.destinationWaypoint?.title
  let lat = currentLeg?.destinationCoordinate.latitude.description
  let lng = currentLeg?.destinationCoordinate.longitude.description
  NSLog(nextDestination! + ", " + lat! + "/" + lng!)
}

Objective-C

GMSRouteLeg *currentSegment = _mapView.navigator.currentRouteLeg;
NSString *nextDestination = currentSegment.destinationWaypoint.title;
CLLocationDegrees lat = currentSegment.destinationCoordinate.latitude;
CLLocationDegrees lng = currentSegment.destinationCoordinate.longitude;
NSLog(@"%@", [NSString stringWithFormat:@"%@, %f/%f", nextDestination, lat, lng]);

การรับเส้นทางที่เดินทางล่าสุด

หากต้องการรับเส้นทางที่เดินทางล่าสุด ให้เรียกใช้ traveledPath ซึ่งจะแสดงผล GMSPath อินสแตนซ์ที่ได้รับการลดความซับซ้อนเพื่อนำจุดที่ซ้ำซ้อนออก (เช่น การเปลี่ยนจุดที่อยู่บนเส้นตรงเดียวกันที่อยู่ติดกันให้เป็นส่วนของเส้นตรงเดียว) เส้นทางนี้จะว่างเปล่าจนกว่าจะเริ่มการนำทาง ตัวอย่างต่อไปนี้แสดงการรับเส้นทางที่เดินทางล่าสุด

Swift

if let navigator = mapView.navigator {
  let latestPath = navigator.traveledPath
  if latestPath.count() > 0 {
    let begin: CLLocationCoordinate2D = latestPath.coordinate(at: 0)
    let current: CLLocationCoordinate2D = latestPath.coordinate(at: latestPath.count() - 1)
    print("Path from (\(begin.latitude),\(begin.longitude)) to (\(current.latitude),\(current.longitude))")
  }
}

Objective-C

GMSPath *latestPath = mapView.navigator.traveledPath;
if (latestPath.count > 0) {
  CLLocationCoordinate2D begin = [latestPath coordinateAtIndex:0];
  CLLocationCoordinate2D current = [latestPath coordinateAtIndex:latestPath.count - 1];
  NSLog(@"Path from %f/%f to %f/%f",
        begin.latitude,
        begin.longitude,
        current.latitude,
        current.longitude);
}