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

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

ภาพรวม

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