Building an all-encompassing package for openrouteservice API in Flutter and Dart
An encapsulation/wrapper made around openrouteservice API for Dart and Flutter projects
Introduction
As a Flutter developer, I was working on a project that required integration with openrouteservice API to generate a route between two coordinates. However, I realized that there was no convenient package available to integrate its services in Flutter or Dart projects. So, I decided to build one myself over the weekend, and I named it open_route_service.
Overview
open_route_service is a package that encapsulates everything openrouteservice API offers. This includes Directions, Elevation, Isochrones, Time-Distance Matrix, Pelias Geocoding, Points of Interest (POIs), Routing Optimizations, and more. The package also includes relevant data models, making it easier to generate routes and directions using their amazing API.
The goal of this package is to provide an all-encompassing solution for developers who need to integrate openrouteservice API in their Flutter or Dart projects. With all of its internal optimizations, the package can help developers save time and effort when building their applications.
Features
The package includes several features, each encapsulating a specific API end-point. Here is an overview of the features and their status:
- ✅ Directions: Route Generation between any two or more coordinates for any mode of transportation.
The Direction data can be used to draw them on a map in a Flutter Application, or anything else you can think of. - ✅ Elevation: Get elevation data around for a specific coordinate or a set of coordinates.
- ✅ Isochrones: Obtain Isochrone (areas of reachability) Data for the locations given. The isochrone is a polygon that encloses a given point and is bounded by a given time.
The isochrone data can be used to draw them on a map in a Flutter Application, or anything else you can think of. - ✅ Time-Distance Matrix: Obtain one-to-many, many-to-one and many-to-many matrices for time and distance. Returns duration or distance matrix for multiple source and destination points.
- ✅ Pelias Geocoding: Resolve input coordinates to addresses and vice versa. Provides functionality for geocoding autocomplete queries, search queries, and reverse geocoding.
- ✅ Points of Interest (POIs): Obtains information about the Points of Interest (POIs) in the area surrounding a geometry which can either be a bounding box, polygon or buffered linestring, or point.
The Points of Interest can be marked on a map in a Flutter Application, or their properties and information visualized in various ways, or anything else you can think of. - ✅ Routing Optimizations: The optimization endpoint solves Vehicle Routing Problems and can be used to schedule multiple vehicles and jobs, respecting time windows, capacities and required skills.
- 🔥 Building, Formatting, and Testing completely automated using Github Actions
Each feature includes unit tests to pinpoint any potential breaking future changes before it is too late. Additionally, the package has minimal external dependencies, making it an isolated system with minimum external constraints.
Usage
- In your flutter/dart project scope, to add the package, run
dart pub add nordigen_integration
2. Import the package where needed:
import 'package:open_route_service/open_route_service.dart';
3. Create a new instance of the class with your openrouteservice API Key:
OpenRouteService openrouteservice = OpenRouteService(apiKey: 'YOUR-API-KEY');
4. Use the handy class methods to easily generate Directions, Isochrones, Time-Distance Matrix, Pelias Geocoding, POIs, Elevation and routing Optimizations etc, letting the package handle all the complex HTTP requests in the background for you.
Example Usage (as of version 1.7.6)
To use the package with the Directions API to generate and draw a Route on a map in a Flutter application:
import 'package:open_route_service/open_route_service.dart';
Future<void> main() async {
// Initialize the openrouteservice with your API key.
final OpenRouteService client = OpenRouteService(apiKey: 'YOUR-API-KEY');
// Example coordinates to test between
const double startLat = 37.4220698;
const double startLng = -122.0862784;
const double endLat = 37.4111466;
const double endLng = -122.0792365;
// Form Route between coordinates
final List<ORSCoordinate> routeCoordinates = await client.directionsRouteCoordsGet(
startCoordinate: ORSCoordinate(latitude: startLat, longitude: startLng),
endCoordinate: ORSCoordinate(latitude: endLat, longitude: endLng),
);
// Print the route coordinates
routeCoordinates.forEach(print);
// Map route coordinates to a list of LatLng (requires google_maps_flutter package)
// to be used in the Map route Polyline.
final List<LatLng> routePoints = routeCoordinates
.map((coordinate) => LatLng(coordinate.latitude, coordinate.longitude))
.toList();
// Create Polyline (requires Material UI for Color)
final Polyline routePolyline = Polyline(
polylineId: PolylineId('route'),
visible: true,
points: routePoints,
color: Colors.red,
width: 4,
);
// Use Polyline to draw route on map or do anything else with the data :)
}
Contributing
The package has been released under an open-source MIT license, making it freely available for use and contributions. To get started, follow this.
If you are a fellow Flutter or Dart developer, feel free to use it and/or contribute, especially if you have an understanding of basic network calls, APIs, data classes, and test-driven development.
Conclusion
open_route_service is an all-encompassing package for openrouteservice API, providing developers with an easy and convenient way to integrate its services into their Flutter or Dart projects. With its encapsulated features and minimal external dependencies, developers can save time and effort while building their applications. If you are a Flutter Developer, I would love to see your contributions.
View the package on pub.dev
Contribute to the package on GitHub
More Information About Openrouteservice
#software #openrouteservice #opensource #flutterdev #fluttercommunity #dart #pubdev #github #opensourcecommunity #apidevelopment #softwaredev #apis