I'm trying to add polyline between two points on the google map, but it doesn't seem to work. I write the code following by the tutorial on youtube https://youtu.be/CT6RkWL3GNE and this website https://dev.to/olayemii/adding-route-paths-polylines-between-two-points-to-google-maps-in-flutter-23o2. I use flutter_polyline_points package for creating polyline. I don't know what's wrong with my code.
This is my code.
const LatLng SOURCE_LOCATION = LatLng(13.652720, 100.493635);
const LatLng DEST_LOCATION = LatLng(13.6640896, 100.4357021);
class Direction extends StatefulWidget {
@override
_DirectionState createState() => _DirectionState();
}
class _DirectionState extends State<Direction> {
Completer<GoogleMapController> mapController = Completer();
Set<Marker> _markers = Set<Marker>();
LatLng currentLocation;
LatLng destinationLocation;
Set<Polyline> _polylines = Set<Polyline>();
List<LatLng> polylineCoordinates = [];
PolylinePoints polylinePoints;
@override
void initState() {
super.initState();
polylinePoints = PolylinePoints();
this.setInitialLocation();
}
void setInitialLocation() {
currentLocation =
LatLng(SOURCE_LOCATION.latitude, SOURCE_LOCATION.longitude);
destinationLocation =
LatLng(DEST_LOCATION.latitude, DEST_LOCATION.longitude);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Direction"),
),
body: GoogleMap(
myLocationEnabled: true,
compassEnabled: false,
tiltGesturesEnabled: false,
polylines: _polylines,
markers: _markers,
onMapCreated: (GoogleMapController controller) {
mapController.complete(controller);
showMarker();
setPolylines();
},
initialCameraPosition: CameraPosition(
target: SOURCE_LOCATION,
zoom: 13,
),
),
);
}
void showMarker() {
setState(() {
_markers.add(Marker(
markerId: MarkerId('sourcePin'),
position: currentLocation,
icon: BitmapDescriptor.defaultMarker,
));
_markers.add(Marker(
markerId: MarkerId('destinationPin'),
position: destinationLocation,
icon: BitmapDescriptor.defaultMarkerWithHue(90),
));
});
}
void setPolylines() async {
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
"<GOOGLE_MAPS_API_KEY_HERE>",
PointLatLng(currentLocation.latitude, currentLocation.longitude),
PointLatLng(
destinationLocation.latitude, destinationLocation.longitude));
if (result.status == 'OK') {
result.points.forEach((PointLatLng point) {
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
});
setState(() {
_polylines.add(Polyline(
width: 10,
polylineId: PolylineId('polyLine'),
color: Color(0xFF08A5CB),
points: polylineCoordinates));
});
}
}
}
Seems like the issue is with your API key. I tried your code using my own API key and I was able to show the polyline (Please see screenshot below). To make it work on your end, make sure that:
Please refer to this documentation to learn more.