กําหนดค่าการแจ้งเตือนมาตรวัดความเร็ว

เมื่อเปิดใช้การนำทาง 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 NavigationFragment.
navigationFragment.setSpeedometerUiOptions(speedometerUiOptionsBuilder.build());
 navigationFragment.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);