From 87db8529d06258721501cc8725a5f72f9e39ad30 Mon Sep 17 00:00:00 2001 From: Jijie LIU Date: Fri, 7 Aug 2020 11:52:26 +0200 Subject: [PATCH] fix bug: wayPoints are not added correctly. New feature: add optimizeWaypoints According to google dev doc, the wayPoints are added as a string. The points are separated by "|". If the points is not stopped over, add prefix "via:" on the point. If the user needs optimize the waypoints, add prefix "optimize:true|" in front of entire waypoints. --- lib/flutter_polyline_points.dart | 19 +++++++++--------- lib/src/network_util.dart | 30 ++++++++++++++++------------ lib/src/utils/polyline_waypoint.dart | 17 +++++++--------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/lib/flutter_polyline_points.dart b/lib/flutter_polyline_points.dart index fd09517..affa8de 100644 --- a/lib/flutter_polyline_points.dart +++ b/lib/flutter_polyline_points.dart @@ -21,15 +21,13 @@ class PolylinePoints { /// which can be used to draw polyline between this two positions /// Future getRouteBetweenCoordinates( - String googleApiKey, - PointLatLng origin, - PointLatLng destination, { - TravelMode travelMode = TravelMode.driving, - List wayPoints = const [], - bool avoidHighways = false, - bool avoidTolls = false, - bool avoidFerries = true, - }) async { + String googleApiKey, PointLatLng origin, PointLatLng destination, + {TravelMode travelMode = TravelMode.driving, + List wayPoints = const [], + bool avoidHighways = false, + bool avoidTolls = false, + bool avoidFerries = true, + bool optimizeWaypoints = false}) async { return await util.getRouteBetweenCoordinates( googleApiKey, origin, @@ -38,7 +36,8 @@ class PolylinePoints { wayPoints, avoidHighways, avoidTolls, - avoidFerries); + avoidFerries, + optimizeWaypoints); } /// Decode and encoded google polyline diff --git a/lib/src/network_util.dart b/lib/src/network_util.dart index 3a5603c..bbb57b0 100644 --- a/lib/src/network_util.dart +++ b/lib/src/network_util.dart @@ -13,15 +13,15 @@ class NetworkUtil { ///Get the encoded string from google directions api /// Future getRouteBetweenCoordinates( - String googleApiKey, - PointLatLng origin, - PointLatLng destination, - TravelMode travelMode, - List wayPoints, - bool avoidHighways, - bool avoidTolls, - bool avoidFerries, - ) async { + String googleApiKey, + PointLatLng origin, + PointLatLng destination, + TravelMode travelMode, + List wayPoints, + bool avoidHighways, + bool avoidTolls, + bool avoidFerries, + bool optimizeWaypoints) async { String mode = travelMode.toString().replaceAll('TravelMode.', ''); PolylineResult result = PolylineResult(); var params = { @@ -34,11 +34,15 @@ class NetworkUtil { "key": googleApiKey }; if (wayPoints.isNotEmpty) { - params.addAll({ - "waypoints": json.encode(List.from(wayPoints.map((e) => e.toMap()))) - }); + List wayPointsArray = wayPoints.map((point) => point.toString()); + String wayPointsString = wayPointsArray.join('|'); + if (optimizeWaypoints) { + wayPointsString = 'optimize:true|$wayPointsString'; + } + params.addAll({"waypoints": wayPointsString}); } - Uri uri = Uri.https("maps.googleapis.com", "maps/api/directions/json", params); + Uri uri = + Uri.https("maps.googleapis.com", "maps/api/directions/json", params); String url = uri.toString(); print('GOOGLE MAPS URL: ' + url); diff --git a/lib/src/utils/polyline_waypoint.dart b/lib/src/utils/polyline_waypoint.dart index 8f4df33..5f6aaef 100644 --- a/lib/src/utils/polyline_waypoint.dart +++ b/lib/src/utils/polyline_waypoint.dart @@ -2,11 +2,10 @@ import 'package:flutter/material.dart'; /// description: /// project: flutter_polyline_points -/// @package: +/// @package: /// @author: dammyololade /// created on: 12/05/2020 class PolylineWayPoint { - /// the location of the waypoint, /// You can specify waypoints using the following values: /// --- Latitude/longitude coordinates (lat/lng): an explicit value pair. (-34.92788%2C138.60008 comma, no space), @@ -20,16 +19,14 @@ class PolylineWayPoint { /// which has the effect of splitting the route into two routes bool stopOver; - PolylineWayPoint({@required this.location, this.stopOver = true}); - Map toMap() => { - "location": location, - "stopover": stopOver - }; - @override String toString() { - return "location=$location,stopover=$stopOver"; + if (stopOver) { + return location; + } else { + return "via:$location"; + } } -} \ No newline at end of file +}