เมื่อเปิดใช้การนำทาง Navigation SDK สำหรับ Android จะแสดงตัวควบคุมขีดจำกัดความเร็วที่มุมซ้ายล่างของแผนที่ ซึ่งจะแสดงขีดจำกัดความเร็วปัจจุบัน หากผู้ใช้ขับรถเร็วเกินขีดจำกัด ตัวควบคุมจะขยายเพื่อแสดงมาตรวัดความเร็วข้างจอแสดงขีดจำกัดความเร็ว และจะทริกเกอร์การแจ้งเตือนเมื่อความเร็วถึงเกณฑ์ที่กำหนด
โดยค่าเริ่มต้น Navigation SDK จะทริกเกอร์การแจ้งเตือนความเร็วเล็กน้อยเมื่อผู้ใช้ขับรถเร็วเกินขีดจำกัด 5 ไมล์ต่อชั่วโมง (หรือ 10 กม./ชม.) และเปลี่ยนสีข้อความมาตรวัดความเร็วเป็นสีแดง และจะทริกเกอร์การแจ้งเตือนความเร็วสำคัญเมื่อผู้ใช้ขับรถเร็วเกินขีดจำกัด 10 ไมล์ต่อชั่วโมง (หรือ 20 กม./ชม.) และเปลี่ยนสีพื้นหลังของมาตรวัดความเร็วเป็นสีแดง
คุณสามารถปรับแต่งทั้งเกณฑ์สำหรับการทริกเกอร์การแจ้งเตือน รวมถึงสีข้อความและสีพื้นหลังที่มาตรวัดความเร็วแสดง นอกจากนี้ คุณยังใช้ Navigation SDK เพื่อให้ข้อมูลความเร็วของผู้ใช้พร้อมใช้งานตามเกณฑ์ที่คุณตั้งไว้ได้ด้วย ตัวอย่างเช่น คุณอาจทำให้ข้อมูลความเร็วพร้อมใช้งานสำหรับผู้ให้บริการร่วมเดินทางเพื่อช่วยให้ผู้ให้บริการกระตุ้นให้ผู้ใช้ปฏิบัติตามขีดจำกัดความเร็วและปรับปรุงความปลอดภัยได้
ปรับแต่งเกณฑ์สำหรับการแจ้งเตือนความเร็ว
คุณสามารถปรับแต่งเกณฑ์การแจ้งเตือนความเร็วสำหรับการแจ้งเตือนความเร็วเล็กน้อยและสำคัญเป็นเปอร์เซ็นต์ที่เกินขีดจำกัดความเร็วของความเร็วปัจจุบันได้ นอกจากนี้ คุณยังระบุระยะเวลาที่เกินเกณฑ์ก่อนที่แผนที่จะแสดงการแจ้งเตือนได้ด้วย
ตัวอย่างโค้ดต่อไปนี้จะตั้งค่าเกณฑ์สำหรับการแจ้งเตือนความเร็วเล็กน้อยเป็น 5% ที่เกินขีดจำกัดความเร็ว และเกณฑ์สำหรับการแจ้งเตือนความเร็วสำคัญเป็น 10% ที่เกินขีดจำกัดความเร็ว โดยจะระบุว่าแผนที่จะแสดงการแจ้งเตือนหลังจากที่เกินเกณฑ์การแจ้งเตือนเป็นเวลา 5 วินาที
float minorSpeedAlertThresholdPercentage = 5; float
majorSpeedAlertThresholdPercentage = 10; double severityUpgradeDurationSeconds =
5;
// Configure SpeedAlertOptions SpeedAlertOptions.Builder
speedAlertOptionsBuilder = new SpeedAlertOptions.Builder();
speedAlertOptionsBuilder.setSpeedAlertThresholdPercentage(
SpeedAlertSeverity.MINOR, minorSpeedAlertThresholdPercentage);
speedAlertOptionsBuilder.setSpeedAlertThresholdPercentage(
SpeedAlertSeverity.MAJOR, majorSpeedAlertThresholdPercentage);
speedAlertOptionsBuilder.setSeverityUpgradeDurationSeconds(severityUpgradeDurationSeconds);
// Set SpeedAlertOptions to Navigator.
navigator.setSpeedAlertOptions(speedAlertOptionsBuilder.build());
ปรับแต่งวิธีที่มาตรวัดความเร็วแสดงการแจ้งเตือนความเร็ว
หากต้องการให้การแจ้งเตือนความเร็วโดดเด่นมากขึ้น คุณสามารถปรับแต่งสีของจอแสดงผลมาตรวัดความเร็วสำหรับการแจ้งเตือนแต่ละระดับได้
ตารางต่อไปนี้แสดงสีเริ่มต้นสำหรับการแจ้งเตือนความเร็วในคลาส NavigationView
| องค์ประกอบ | สี |
|---|---|
| MinorSpeedAlertBackgroundColorDayMode | 0xffffff(สีขาว) |
| MinorSpeedAlertBackgroundColorNightMode | 0x000000 |
| MinorSpeedAlertTextColorDayMode | 0xd93025 |
| MinorSpeedAlertTextColorNightMode | 0xd93025 |
| MajorSpeedAlertBackgroundColorDayMode | 0xd93025 |
| MajorSpeedAlertBackgroundColorNightMode | 0xd93025 |
| MajorSpeedAlertTextColorDayMode | 0xffffff(สีขาว) |
| MajorSpeedAlertTextColorNightMode | 0xffffff(สีขาว) |
คุณสามารถระบุสีข้อความและสีพื้นหลังของมาตรวัดความเร็วสำหรับการแจ้งเตือนความเร็วเล็กน้อยและสำคัญได้ดังนี้
SpeedometerUiOptions speedometerUiOptions = new SpeedometerUiOptions.Builder()
.setBackgroundColorDayMode(MINOR, some_color)
.setBackgroundColorNightMode(MINOR, some_color) .setTextColorDayMode(MINOR,
some_color) .setTextColorNightMode(MINOR, some_color)
.setBackgroundColorDayMode(MAJOR, some_color)
.setBackgroundColorNightMode(MAJOR, some_color) .setTextColorDayMode(MAJOR,
some_color) .setTextColorNightMode(MAJOR, some_color) .build();
// Set SpeedometerUiOptions to NavigationView.
navigationView.setSpeedometerUiOptions(speedometerUiOptionsBuilder.build());
navigationView.setSpeedometerEnabled(true);
// Set SpeedometerUiOptions to SupportNavigationFragment.
supportNavigationFragment.setSpeedometerUiOptions(speedometerUiOptionsBuilder.build());
supportNavigationFragment.setSpeedometerEnabled(true);
รับข้อมูลความเร็วจากผู้ใช้
หากแอปพลิเคชันของคุณต้องแชร์ข้อมูลเกี่ยวกับความเร็วของผู้ใช้ คุณสามารถใช้ Navigation SDK เพื่อให้ข้อมูลความเร็วของผู้ใช้พร้อมใช้งานได้ด้วย ตัวอย่างเช่น ฟีเจอร์นี้มีประโยชน์สำหรับแอปพลิเคชันร่วมเดินทาง ซึ่งผู้ให้บริการอาจต้องการตรวจสอบการขับรถเร็วเกินไปของผู้ใช้เพื่อปรับปรุงความปลอดภัย นอกจากนี้ คุณยังทำได้โดยไม่ต้องแสดงมุมมองการนำทางแก่ผู้ใช้ในแอป
ตัวอย่างเช่น ตัวอย่างต่อไปนี้จะแชร์ข้อมูลความเร็วเมื่อความเร็วเป็นเปอร์เซ็นต์ที่ระบุที่เกินขีดจำกัดความเร็ว
// Existing flow for creating Navigator. NavigationApi.getNavigator();
// Set the SpeedAlertOptions for the MAJOR and MINOR alerts. (Note that the //
severityUpgradeDurationSeconds field is by design not used in this flow.)
SpeedAlertOptions speedAlertOptions = ...;
navigator.setSpeedAlertOptions(speedAlertOptions);
// Implement SpeedingListener. SpeedingListener speedingListener = new
SpeedingListener() { @Override public void onSpeedingUpdated(float
percentageAboveLimit, SpeedAlertSeverity speedAlertSeverity) { ... } };
// Set speedingListener to Navigator.
navigator.setSpeedingListener(speedingListener);