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

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

ภาพรวม

หากต้องการดูข้อมูลเกี่ยวกับเส้นทางปัจจุบัน ให้รับพร็อพเพอร์ตี้ที่เหมาะสมจากอินสแตนซ์ 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);
}