Skip to content

Commit

Permalink
(feat) modify route params in launch config
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuyulv committed Aug 7, 2024
1 parent f6afd53 commit 49355ce
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 96 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
## v0.7.1, August 7, 2024
* Optimization: Simplified Route Switching Handling, by removing unnecessary list operations, the new code is more efficient.
* Previous Implementation:
* When a route was selected, the code moved the selected route to the beginning of the list and redrew the routes.
* New Implementation:
* Simplified the logic to just set primaryIndex to selectedRouteIndex.
```
navNextBillionMap.addRouteSelectedListener(coordinates, (selectedRouteIndex) {
if (routes.isNotEmpty) {
primaryIndex = selectedRouteIndex;
}
});
```
* Bug Fix: Primary Route Selection
* Issue: The selected route was not being used as the primary route; the default behavior was always using routes.first.
* Fix: Updated the configuration to support the selected route as the primary route.
```
NavigationLauncherConfig config = NavigationLauncherConfig(route: selectedRoute, routes: routes);
```
## v0.7.0, July 23, 2024
* Update the android navigation native framework to 1.3.6 to fix the speedometer view shown issue on the [NBNavigationView]
* Fix speedometer not shown on Android when use [NBNavigationView]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,21 @@ import android.text.TextUtils
object Convert {
fun convertLauncherConfig(arguments: Map<*, *>): NavLauncherConfig.Builder? {
val routesJson = arguments["routes"] as? List<*>
if (!routesJson.isNullOrEmpty()) {
val routes = routesJson.map { json -> DirectionsRoute.fromJson(json as String) }
val singleRouteJson = arguments["route"] as? String
if (!TextUtils.isEmpty(singleRouteJson)) {
val route = DirectionsRoute.fromJson(singleRouteJson)
var routes: MutableList<DirectionsRoute> = mutableListOf()
if (!routesJson.isNullOrEmpty()) {
routes = routesJson.map { json -> DirectionsRoute.fromJson(json as String) }.toMutableList()
val primaryIndex = routes.indexOfFirst { it.geometry() == route.geometry() }
if (primaryIndex >= 0) {
routes.removeAt(primaryIndex)
routes.add(0, route)
}
} else {
routes.add(route)
}

val configBuilder = NavLauncherConfig.builder(routes[0])
configBuilder.routes(routes)

Expand Down
21 changes: 9 additions & 12 deletions example/lib/custom_view_on_navigation_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class CustomViewOnNavigationViewState
// Speed limit values for each step
Map<String, String> speedLimits = {};

var primaryIndex = 0;

void _onMapCreated(NextbillionMapController controller) {
this.controller = controller;
}
Expand Down Expand Up @@ -76,17 +78,12 @@ class CustomViewOnNavigationViewState
_onMapClick(Point<double> point, LatLng coordinates) {
navNextBillionMap.addRouteSelectedListener(coordinates,
(selectedRouteIndex) {
if (routes.isNotEmpty && selectedRouteIndex != 0) {
var selectedRoute = routes[selectedRouteIndex];
routes.removeAt(selectedRouteIndex);
routes.insert(0, selectedRoute);
setState(() {
routes = routes;
});
navNextBillionMap.drawRoute(routes);
// You need to recalculate the speed limit for the new selected route
_calculateSpeedLimit(selectedRoute);
}
if (routes.isNotEmpty) {
primaryIndex = selectedRouteIndex;
DirectionsRoute selectedRoute = routes[selectedRouteIndex];
// You need to recalculate the speed limit for the new selected route
_calculateSpeedLimit(selectedRoute);
}
});
}

Expand Down Expand Up @@ -230,7 +227,7 @@ class CustomViewOnNavigationViewState

NavigationLauncherConfig _buildNavigationViewConfig() {
NavigationLauncherConfig config =
NavigationLauncherConfig(route: routes.first, routes: routes);
NavigationLauncherConfig(route: routes[primaryIndex], routes: routes);
config.locationLayerRenderMode = LocationLayerRenderMode.gps;
// config.shouldSimulateRoute = true;
config.themeMode = NavigationThemeMode.system;
Expand Down
15 changes: 5 additions & 10 deletions example/lib/draw_route_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DrawRouteLineState extends State<DrawRouteLine> {

bool enableAlternativeRoutes = true;
bool enableRouteDurationSymbol = true;
var primaryIndex = 0;

void _onMapCreated(NextbillionMapController controller) {
this.controller = controller;
Expand All @@ -43,15 +44,9 @@ class DrawRouteLineState extends State<DrawRouteLine> {
_onMapClick(Point<double> point, LatLng coordinates) {
navNextBillionMap.addRouteSelectedListener(coordinates,
(selectedRouteIndex) {
if (routes.isNotEmpty && selectedRouteIndex != 0) {
var selectedRoute = routes[selectedRouteIndex];
routes.removeAt(selectedRouteIndex);
routes.insert(0, selectedRoute);
setState(() {
routes = routes;
});
navNextBillionMap.drawRoute(routes);
}
if (routes.isNotEmpty) {
primaryIndex = selectedRouteIndex;
}
});
}

Expand Down Expand Up @@ -124,7 +119,7 @@ class DrawRouteLineState extends State<DrawRouteLine> {
void _startNavigation() {
if (routes.isEmpty) return;
NavigationLauncherConfig config =
NavigationLauncherConfig(route: routes.first, routes: routes);
NavigationLauncherConfig(route: routes[primaryIndex], routes: routes);
config.locationLayerRenderMode = LocationLayerRenderMode.gps;
config.themeMode = NavigationThemeMode.system;
config.useCustomNavigationStyle = false;
Expand Down
15 changes: 5 additions & 10 deletions example/lib/draw_route_line_with_raw_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class DrawRouteLineState extends State<DrawRouteLineWithRawJson> {

bool enableAlternativeRoutes = true;
bool enableRouteDurationSymbol = true;
var primaryIndex = 0;

void _onMapCreated(NextbillionMapController controller) {
this.controller = controller;
Expand All @@ -45,15 +46,9 @@ class DrawRouteLineState extends State<DrawRouteLineWithRawJson> {
_onMapClick(Point<double> point, LatLng coordinates) {
navNextBillionMap.addRouteSelectedListener(coordinates,
(selectedRouteIndex) {
if (routes.isNotEmpty && selectedRouteIndex != 0) {
var selectedRoute = routes[selectedRouteIndex];
routes.removeAt(selectedRouteIndex);
routes.insert(0, selectedRoute);
setState(() {
routes = routes;
});
navNextBillionMap.drawIndependentRoutes(routes);
}
if (routes.isNotEmpty) {
primaryIndex = selectedRouteIndex;
}
});
}

Expand Down Expand Up @@ -111,7 +106,7 @@ class DrawRouteLineState extends State<DrawRouteLineWithRawJson> {
void _startNavigation() {
if (routes.isEmpty) return;
NavigationLauncherConfig config =
NavigationLauncherConfig(route: routes.first, routes: routes);
NavigationLauncherConfig(route: routes[primaryIndex], routes: routes);
config.locationLayerRenderMode = LocationLayerRenderMode.gps;
config.themeMode = NavigationThemeMode.system;
config.useCustomNavigationStyle = false;
Expand Down
15 changes: 5 additions & 10 deletions example/lib/full_navigation_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class FullNavigationExampleState extends State<FullNavigationExample> {
String locationTrackImage = "assets/location_on.png";
UserLocation? currentLocation;
List<LatLng> waypoints = [];
var primaryIndex = 0;

void _onMapCreated(NextbillionMapController controller) {
this.controller = controller;
Expand Down Expand Up @@ -53,15 +54,9 @@ class FullNavigationExampleState extends State<FullNavigationExample> {
_onMapClick(Point<double> point, LatLng coordinates) {
navNextBillionMap.addRouteSelectedListener(coordinates,
(selectedRouteIndex) {
if (routes.isNotEmpty && selectedRouteIndex != 0) {
var selectedRoute = routes[selectedRouteIndex];
routes.removeAt(selectedRouteIndex);
routes.insert(0, selectedRoute);
setState(() {
routes = routes;
});
navNextBillionMap.drawRoute(routes);
}
if (routes.isNotEmpty) {
primaryIndex = selectedRouteIndex;
}
});
}

Expand Down Expand Up @@ -232,7 +227,7 @@ class FullNavigationExampleState extends State<FullNavigationExample> {
void _startNavigation() {
if (routes.isEmpty) return;
NavigationLauncherConfig config =
NavigationLauncherConfig(route: routes.first, routes: routes);
NavigationLauncherConfig(route: routes[primaryIndex], routes: routes);
config.locationLayerRenderMode = LocationLayerRenderMode.gps;
config.shouldSimulateRoute = false;
config.themeMode = NavigationThemeMode.system;
Expand Down
15 changes: 5 additions & 10 deletions example/lib/launch_embedded_navigation_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class LaunchEmbeddedNavigationViewState
UserLocation? currentLocation;
List<LatLng> waypoints = [];
bool showArrivalDialog = true;
var primaryIndex = 0;

void _onMapCreated(NextbillionMapController controller) {
this.controller = controller;
Expand Down Expand Up @@ -66,15 +67,9 @@ class LaunchEmbeddedNavigationViewState
_onMapClick(Point<double> point, LatLng coordinates) {
navNextBillionMap.addRouteSelectedListener(coordinates,
(selectedRouteIndex) {
if (routes.isNotEmpty && selectedRouteIndex != 0) {
var selectedRoute = routes[selectedRouteIndex];
routes.removeAt(selectedRouteIndex);
routes.insert(0, selectedRoute);
setState(() {
routes = routes;
});
navNextBillionMap.drawRoute(routes);
}
if (routes.isNotEmpty) {
primaryIndex = selectedRouteIndex;
}
});
}

Expand Down Expand Up @@ -168,7 +163,7 @@ class LaunchEmbeddedNavigationViewState

NavigationLauncherConfig _buildNavigationViewConfig() {
NavigationLauncherConfig config =
NavigationLauncherConfig(route: routes.first, routes: routes);
NavigationLauncherConfig(route: routes[primaryIndex], routes: routes);
config.locationLayerRenderMode = LocationLayerRenderMode.gps;
config.shouldSimulateRoute = true;
config.themeMode = NavigationThemeMode.system;
Expand Down
41 changes: 24 additions & 17 deletions ios/Classes/Convert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,20 @@ class Convert {
guard let routeOptions = routeOptions else {
return []
}

if let routeJosn = launcherConfig["routes"] as? [String], !routeJosn.isEmpty {
if let singleRouteJson = launcherConfig["route"] as? String, !singleRouteJson.isEmpty {
let singleRoute = modifyRoute(routeOptions: routeOptions, routeJson: singleRouteJson)
var routes: [Route] = []
routeJosn.forEach { jsonString in
let json = jsonStringToDictionary(jsonString)
let countryCode = json["countryCode"] as? String ?? ""
let route = Route.init(json: json, waypoints: routeOptions.waypoints, options: routeOptions, countryCode: countryCode)
route.speechLocale = routeOptions.locale
route.modifyRoute()
routes.append(route)
if let routeJosn = launcherConfig["routes"] as? [String], !routeJosn.isEmpty {
routeJosn.forEach { jsonString in
let route = modifyRoute(routeOptions: routeOptions, routeJson: jsonString)
routes.append(route)
}
if let primaryIndex = routes.firstIndex(where: { $0.shape == singleRoute.shape } ), primaryIndex >= 0 {
routes.remove(at: primaryIndex)
routes.insert(singleRoute, at: 0)
}
} else {
routes.append(singleRoute)
}
return routes
}
Expand All @@ -201,20 +205,23 @@ class Convert {

}

class func modifyRoute(routeOptions: NBNavRouteOptions, routeJson: String) -> Route {
let json = jsonStringToDictionary(routeJson)
let countryCode = json["countryCode"] as? String ?? ""
let route = Route.init(json: json, waypoints: routeOptions.waypoints, options: routeOptions, countryCode: countryCode)
route.speechLocale = routeOptions.locale
route.modifyRoute()

return route
}

class func convertDirectionsRoute(arguments: [String: Any]) -> Route? {
if let options = arguments["routeOptions"] as? String, let routeJson = arguments["route"] as? String {
let routeOptions = convertRouteRequestParams(arguments: options)
guard let routeOptions = routeOptions else {
return nil
}

let json = jsonStringToDictionary(routeJson)
let countryCode = json["countryCode"] as? String ?? ""
let route = Route.init(json: json, waypoints: routeOptions.waypoints, options: routeOptions, countryCode: countryCode)
route.speechLocale = routeOptions.locale
route.modifyRoute()

return route
return modifyRoute(routeOptions: routeOptions, routeJson: routeJson)
}
return nil

Expand Down
Loading

0 comments on commit 49355ce

Please sign in to comment.