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

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

ภาพรวม

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