Reputation: 3
I am trying to get the distance between points for a pick-up and drop-off; however, every time use the method getRouteBetweenCoordinates I get the following error:
The method 'RouteBetweenCoordinatesRequest' isn't defined for the type '_HomeScreenState'.Try correcting the name to the name of an existing method, or defining a method named 'RouteBetweenCoordinatesRequest'.
Here is my code:
Future<void> _calculateRoute() async {
if (_pickupLocation == null || _deliveryLocation == null) return;
final polylinePoints = PolylinePoints();
final request = RouteBetweenCoordinatesRequest(
apiKey: 'YOUR_API_KEY',
origin:
PointLatLng(_pickupLocation!.latitude, _pickupLocation!.longitude),
destination: PointLatLng(
_deliveryLocation!.latitude, _deliveryLocation!.longitude),
);
final result =
await polylinePoints.getRouteBetweenCoordinates(request: request);
if (result.points.isNotEmpty) {
final points = result.points
.map((point) => LatLng(point.latitude, point.longitude))
.toList();
setState(() {
_polylines = {
Polyline(
polylineId: const PolylineId('route'),
points: points,
color: Colors.blue,
width: 4,
)
};
_distance = _calculateDistanceBetween(
_pickupLocation!.latitude,
_pickupLocation!.longitude,
_deliveryLocation!.latitude,
_deliveryLocation!.longitude,
);
_totalCost = _distance * 15;
_showRoute = true;
});
}
}
double _calculateDistanceBetween(
double lat1, double lon1, double lat2, double lon2) {
const double earthRadius = 6371;
final double dLat = _degreesToRadians(lat2 - lat1);
final double dLon = _degreesToRadians(lon2 - lon1);
final double a = sin(dLat / 2) * sin(dLat / 2) +
cos(_degreesToRadians(lat1)) *
cos(_degreesToRadians(lat2)) *
sin(dLon / 2) *
sin(dLon / 2);
final double c = 2 * atan2(sqrt(a), sqrt(1 - a));
return earthRadius * c;
}
double _degreesToRadians(double degrees) => degrees * (pi / 180);
void _confirmOrder() => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PaymentScreen(
distance: _distance,
totalCost: _totalCost,
),
),
);
Upvotes: 0
Views: 25
Reputation: 524
Base on the error, it looks like you missed importing a file containing RouteBetweenCoordinatesRequest class but then, that's not the main issue. what you really want to do is instantiating the Polylinepoint class and then call getRouteBetweenCoordinates method of the class:
The correct version of your _calculateRoute
code will likely be:
Future<void> _calculateRoute() async {
if (_pickupLocation == null || _deliveryLocation == null) return;
final polylinePoints = PolylinePoints();
final request = polylinePoints.getRouteBetweenCoordinates( # Changes here
apiKey: 'YOUR_API_KEY',
origin:
PointLatLng(_pickupLocation!.latitude, _pickupLocation!.longitude),
destination: PointLatLng(
_deliveryLocation!.latitude, _deliveryLocation!.longitude),
);
final result =
await polylinePoints.getRouteBetweenCoordinates(request: request);
if (result.points.isNotEmpty) {
final points = result.points
.map((point) => LatLng(point.latitude, point.longitude))
.toList();
setState(() {
_polylines = {
Polyline(
polylineId: const PolylineId('route'),
points: points,
color: Colors.blue,
width: 4,
)
};
_distance = _calculateDistanceBetween(
_pickupLocation!.latitude,
_pickupLocation!.longitude,
_deliveryLocation!.latitude,
_deliveryLocation!.longitude,
);
_totalCost = _distance * 15;
_showRoute = true;
});
}
}
Upvotes: 0