diff --git a/CHANGELOG.md b/CHANGELOG.md index 13f2d5b..27c809c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [0.2.1] - 05-04-2020. +Breaking change, the response object has been refined to a more +suitable object that contains the status of the api and the error message. ## [0.2.0] - 05-04-2020. Add travel mode, Fixed unhandled error exception. diff --git a/README.md b/README.md index 37d256d..6260a18 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,13 @@ import 'package:flutter_polyline_points/flutter_polyline_points.dart'; ``` ## First method -Get the list of points by Geo-coordinate +Get the list of points by Geo-coordinate, this return an instance of PolylineResult, which +contains the status of the api, the errorMessage, and the list of decoded points. ```dart PolylinePoints polylinePoints = PolylinePoints(); -List result = await polylinePoints.getRouteBetweenCoordinates(googleAPiKey, +PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(googleAPiKey, _originLatitude, _originLongitude, _destLatitude, _destLongitude); -print(result); +print(result.points); ``` ## Second method @@ -40,4 +41,4 @@ See the example directory for a complete sample app ## Hint kindly ensure you use a valid google api key, -[for help generating api key for your project click this link](https://developers.google.com/maps/documentation/directions/get-api-key) +[If you need help generating api key for your project click this link](https://developers.google.com/maps/documentation/directions/get-api-key) diff --git a/example/lib/main.dart b/example/lib/main.dart index bd0b3c7..a93e1b7 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -97,15 +97,15 @@ class _MapScreenState extends State { } _getPolyline() async { - List result = await polylinePoints.getRouteBetweenCoordinates( + PolylineResult result = await polylinePoints.getRouteBetweenCoordinates( Constants.API_KEY, - _originLatitude, - _originLongitude, - _destLatitude, - _destLongitude, - travelMode: TravelMode.driving); - if (result.isNotEmpty) { - result.forEach((PointLatLng point) { + PointLatLng(_originLatitude, _originLongitude), + PointLatLng(_destLatitude, _destLongitude), + travelMode: TravelMode.driving, + wayPoints: [PolylineWayPoint(location: "Sabo, Yaba Lagos Nigeria")] + ); + if (result.points.isNotEmpty) { + result.points.forEach((PointLatLng point) { polylineCoordinates.add(LatLng(point.latitude, point.longitude)); }); } diff --git a/lib/flutter_polyline_points.dart b/lib/flutter_polyline_points.dart index db1ad0a..4ac763a 100644 --- a/lib/flutter_polyline_points.dart +++ b/lib/flutter_polyline_points.dart @@ -2,10 +2,17 @@ library flutter_polyline_points; import 'dart:convert'; -import 'package:http/http.dart' as http; +import 'package:flutter_polyline_points/src/utils/polyline_result.dart'; +import 'package:flutter_polyline_points/src/utils/polyline_waypoint.dart'; +import 'package:flutter_polyline_points/src/utils/travel_modes.dart'; +import 'src/PointLatLng.dart'; +import 'src/network_util.dart'; -part 'src/PointLatLng.dart'; -part 'src/network_util.dart'; +export 'src/utils/travel_modes.dart'; +export 'src/utils/polyline_waypoint.dart'; +export 'src/network_util.dart'; +export 'src/PointLatLng.dart'; +export 'src/utils/polyline_result.dart'; class PolylinePoints { NetworkUtil util = NetworkUtil(); @@ -13,15 +20,25 @@ class PolylinePoints { /// Get the list of coordinates between two geographical positions /// which can be used to draw polyline between this two positions /// - Future> getRouteBetweenCoordinates( - String googleApiKey, - double originLat, - double originLong, - double destLat, - double destLong, - {TravelMode travelMode = TravelMode.driving}) async { + 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 { return await util.getRouteBetweenCoordinates( - googleApiKey, originLat, originLong, destLat, destLong, travelMode); + googleApiKey, + origin, + destination, + travelMode, + wayPoints, + avoidHighways, + avoidTolls, + avoidFerries); } /// Decode and encoded google polyline diff --git a/lib/src/PointLatLng.dart b/lib/src/PointLatLng.dart index 619da23..3375497 100644 --- a/lib/src/PointLatLng.dart +++ b/lib/src/PointLatLng.dart @@ -1,4 +1,4 @@ -part of flutter_polyline_points; + /// A pair of latitude and longitude coordinates, stored as degrees. class PointLatLng { diff --git a/lib/src/network_util.dart b/lib/src/network_util.dart index 85c7059..4502439 100644 --- a/lib/src/network_util.dart +++ b/lib/src/network_util.dart @@ -1,46 +1,61 @@ -part of flutter_polyline_points; +import 'dart:convert'; -enum TravelMode { driving, bicycling, transit, walking } +import '../src/utils/polyline_waypoint.dart'; +import '../src/utils/travel_modes.dart'; +import '../src/PointLatLng.dart'; +import 'package:http/http.dart' as http; + +import 'utils/polyline_result.dart'; class NetworkUtil { static const String STATUS_OK = "ok"; ///Get the encoded string from google directions api /// - Future> getRouteBetweenCoordinates( - String googleApiKey, - double originLat, - double originLong, - double destLat, - double destLong, - TravelMode travelMode) async { + Future getRouteBetweenCoordinates( + String googleApiKey, + PointLatLng origin, + PointLatLng destination, + TravelMode travelMode, + List wayPoints, + bool avoidHighways, + bool avoidTolls, + bool avoidFerries, + ) async { String mode = travelMode.toString().replaceAll('TravelMode.', ''); - List polylinePoints = []; - String url = - "https://maps.googleapis.com/maps/api/directions/json?origin=" + - originLat.toString() + - "," + - originLong.toString() + - "&destination=" + - destLat.toString() + - "," + - destLong.toString() + - "&mode=$mode" + - "&key=$googleApiKey"; + PolylineResult result = PolylineResult(); + var params = { + "origin": "${origin.latitude},${origin.longitude}", + "destination": "${destination.latitude},${destination.longitude}", + "travelMode": mode, + "avoidHighways": "$avoidHighways", + "avoidFerries": "$avoidFerries", + "avoidTolls": "$avoidTolls", + "key": googleApiKey + }; + if (wayPoints.isNotEmpty) { + params.addAll({ + "waypoints": json.encode(List.from(wayPoints.map((e) => e.toMap()))) + }); + } + Uri uri = Uri.https("maps.googleapis.com", "maps/api/directions/json", params); + + String url = uri.toString(); print('GOOGLE MAPS URL: ' + url); var response = await http.get(url); if (response?.statusCode == 200) { var parsedJson = json.decode(response.body); + result.status = parsedJson["status"]; if (parsedJson["status"]?.toLowerCase() == STATUS_OK && parsedJson["routes"] != null && parsedJson["routes"].isNotEmpty) { - polylinePoints = decodeEncodedPolyline( + result.points = decodeEncodedPolyline( parsedJson["routes"][0]["overview_polyline"]["points"]); } else { - throw Exception(parsedJson["error_message"]); + result.errorMessage = parsedJson["error_message"]; } } - return polylinePoints; + return result; } ///decode the google encoded string using Encoded Polyline Algorithm Format diff --git a/lib/src/utils/polyline_result.dart b/lib/src/utils/polyline_result.dart new file mode 100644 index 0000000..325470a --- /dev/null +++ b/lib/src/utils/polyline_result.dart @@ -0,0 +1,24 @@ +import '../../flutter_polyline_points.dart'; + +/// description: +/// project: flutter_polyline_points +/// @package: +/// @author: dammyololade +/// created on: 13/05/2020 +class PolylineResult { + + /// the api status retuned from google api + /// + /// returns OK if the api call is successful + String status; + + /// list of decoded points + List points; + + /// the error message returned from google, if none, the result will be empty + String errorMessage; + + PolylineResult({this.status, this.points = const [], this.errorMessage = ""}); + + +} \ No newline at end of file diff --git a/lib/src/utils/polyline_waypoint.dart b/lib/src/utils/polyline_waypoint.dart new file mode 100644 index 0000000..01dff45 --- /dev/null +++ b/lib/src/utils/polyline_waypoint.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; + +/// description: +/// project: flutter_polyline_points +/// @package: +/// @author: dammyololade +/// created on: 12/05/2020 +class PolylineWayPoint { + + /// specifies the location of the waypoint, + /// as a LatLng, as a google.maps.Place object + /// or as a String which will be geocoded. + dynamic location; + + /// is a boolean which indicates that the waypoint is a stop on the route, + /// which has the effect of splitting the route into two routes + bool stopOver; + + + PolylineWayPoint({@required this.location, this.stopOver = true}); + + Map toMap() => { + "location": location.toString(), + "stopover": stopOver + }; + + @override + String toString() { + return "location=${location.toString()},stopover=$stopOver"; + } +} \ No newline at end of file diff --git a/lib/src/utils/travel_modes.dart b/lib/src/utils/travel_modes.dart new file mode 100644 index 0000000..19340ba --- /dev/null +++ b/lib/src/utils/travel_modes.dart @@ -0,0 +1,6 @@ +/// description: +/// project: flutter_polyline_points +/// @package: +/// @author: dammyololade +/// created on: 12/05/2020 +enum TravelMode { driving, bicycling, transit, walking } \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index c836d6f..14f57ec 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_polyline_points description: A flutter package to get polyline points by either passing the coordinates or google encoded polyline string -version: 0.2.0 +version: 0.2.1 homepage: https://github.com/Dammyololade/flutter_polyline_points environment: diff --git a/test/flutter_polyline_points_test.dart b/test/flutter_polyline_points_test.dart index 01dce08..041f564 100644 --- a/test/flutter_polyline_points_test.dart +++ b/test/flutter_polyline_points_test.dart @@ -1,4 +1,5 @@ import 'package:flutter_polyline_points/src/Constants.dart'; +import 'package:flutter_polyline_points/src/utils/polyline_result.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_polyline_points/flutter_polyline_points.dart'; @@ -6,10 +7,11 @@ import 'package:flutter_polyline_points/flutter_polyline_points.dart'; void main() { test('get list of coordinates from two geographical positions', () async { final polylinePoints = PolylinePoints(); - List points = await polylinePoints.getRouteBetweenCoordinates( - Constants.API_KEY, 6.5212402, 3.3679965, 6.595680, 3.337030, + PolylineResult result = await polylinePoints.getRouteBetweenCoordinates( + Constants.API_KEY, PointLatLng(6.5212402, 3.3679965), + PointLatLng(6.595680, 3.337030), travelMode: TravelMode.driving); - assert(points.isNotEmpty == true); + assert(result.points.isNotEmpty == true); }); test('get list of coordinates from an encoded String', () {