เมื่อเปิดใช้การนำทางแล้ว 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);