เมื่อเปิดใช้งานการนำทาง SDK การนำทางสำหรับ Android จะแสดงการควบคุมขีดจำกัดความเร็วที่มุมล่างซ้ายของแผนที่ซึ่งจะแสดงขีดจำกัดความเร็วปัจจุบัน หากผู้ขับขี่ใช้ความเร็วเกินขีดจำกัด ตัวควบคุมจะขยายเพื่อแสดงมาตรวัดความเร็วข้างจอแสดงผลขีดจำกัดความเร็ว และทริกเกอร์การแจ้งเตือนเมื่อความเร็วถึงเกณฑ์ที่กำหนด
โดยค่าเริ่มต้น 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);