Skip to content

Commit

Permalink
Merge pull request #12 from Dammyololade/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Dammyololade authored May 12, 2020
2 parents 35c7bf4 + 6299b39 commit cc3356a
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 52 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<PointLatLng> result = await polylinePoints.getRouteBetweenCoordinates(googleAPiKey,
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(googleAPiKey,
_originLatitude, _originLongitude, _destLatitude, _destLongitude);
print(result);
print(result.points);
```

## Second method
Expand All @@ -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)
16 changes: 8 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ class _MapScreenState extends State<MapScreen> {
}

_getPolyline() async {
List<PointLatLng> 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));
});
}
Expand Down
39 changes: 28 additions & 11 deletions lib/flutter_polyline_points.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,43 @@ 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();

/// Get the list of coordinates between two geographical positions
/// which can be used to draw polyline between this two positions
///
Future<List<PointLatLng>> getRouteBetweenCoordinates(
String googleApiKey,
double originLat,
double originLong,
double destLat,
double destLong,
{TravelMode travelMode = TravelMode.driving}) async {
Future<PolylineResult> getRouteBetweenCoordinates(
String googleApiKey,
PointLatLng origin,
PointLatLng destination, {
TravelMode travelMode = TravelMode.driving,
List<PolylineWayPoint> 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
Expand Down
2 changes: 1 addition & 1 deletion lib/src/PointLatLng.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of flutter_polyline_points;


/// A pair of latitude and longitude coordinates, stored as degrees.
class PointLatLng {
Expand Down
63 changes: 39 additions & 24 deletions lib/src/network_util.dart
Original file line number Diff line number Diff line change
@@ -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<List<PointLatLng>> getRouteBetweenCoordinates(
String googleApiKey,
double originLat,
double originLong,
double destLat,
double destLong,
TravelMode travelMode) async {
Future<PolylineResult> getRouteBetweenCoordinates(
String googleApiKey,
PointLatLng origin,
PointLatLng destination,
TravelMode travelMode,
List<PolylineWayPoint> wayPoints,
bool avoidHighways,
bool avoidTolls,
bool avoidFerries,
) async {
String mode = travelMode.toString().replaceAll('TravelMode.', '');
List<PointLatLng> 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
Expand Down
24 changes: 24 additions & 0 deletions lib/src/utils/polyline_result.dart
Original file line number Diff line number Diff line change
@@ -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<PointLatLng> points;

/// the error message returned from google, if none, the result will be empty
String errorMessage;

PolylineResult({this.status, this.points = const [], this.errorMessage = ""});


}
31 changes: 31 additions & 0 deletions lib/src/utils/polyline_waypoint.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> toMap() => {
"location": location.toString(),
"stopover": stopOver
};

@override
String toString() {
return "location=${location.toString()},stopover=$stopOver";
}
}
6 changes: 6 additions & 0 deletions lib/src/utils/travel_modes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// description:
/// project: flutter_polyline_points
/// @package:
/// @author: dammyololade
/// created on: 12/05/2020
enum TravelMode { driving, bicycling, transit, walking }
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
8 changes: 5 additions & 3 deletions test/flutter_polyline_points_test.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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';

void main() {
test('get list of coordinates from two geographical positions', () async {
final polylinePoints = PolylinePoints();
List<PointLatLng> 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', () {
Expand Down

0 comments on commit cc3356a

Please sign in to comment.