I’m vague at such stuffs of Google Maps. While the markers are available on the map, the polyline doesn’t appear in the screen and I don’t seem to be able to place markers outside of Widget build.
class _MapScreenState extends State<MapScreen> {
late GoogleMapController mapController;
LatLng? currentLocation;
double? currentlat; double? currentlong;
LatLng destination = const LatLng(4.66968, 101.07338);
bool _isLoading = true;
Set<Marker> markers = {};
PolylinePoints polylinePoints = PolylinePoints();
Map<PolylineId, Polyline> polylines = {};
List<LatLng> polylineCoordinates = [];
@override
void initState() {
super.initState();
getLocation();
makeLines();
addPolyLine();
}
getLocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
double lat = position.latitude;
double long = position.longitude;
LatLng location = LatLng(lat, long);
currentlat = lat;
currentlong = long;
setState(() {
currentLocation = location;
_isLoading = false;
});
print("Current Lat: $lat, Long: $long");
}
addPolyLine() {
PolylineId id = const PolylineId("poly");
Polyline polyline = Polyline(
polylineId: id,
color: Colors.blue,
points: polylineCoordinates,
width: 3,
);
polylines[id] = polyline;
}
void makeLines() async {
await polylinePoints
.getRouteBetweenCoordinates(
'API KEY',
PointLatLng(currentlat??0, currentlong??0),
PointLatLng(destination.latitude, destination.longitude), //End LATLANG
travelMode: TravelMode.transit,
)
.then((value) {
value.points.forEach((PointLatLng point) {
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
});
}).then((value) {
addPolyLine();
});
}
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
Marker destinationMarker = Marker(
markerId: const MarkerId('targetLocation'),
position: destination,
infoWindow: const InfoWindow(title: 'Destination'),
);
Marker currentLocationMarker = Marker(
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue),
markerId: const MarkerId('currentLocation'),
position: LatLng(currentlat??0, currentlong??0),
infoWindow: const InfoWindow(title: 'Current Location'),
);
return Scaffold(
appBar: AppBar(
title: const Text('Map'),
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: Center(
child: Column(
children: [
Expanded(
child: GoogleMap(
myLocationEnabled: true,
onMapCreated: _onMapCreated,
markers: <Marker>{destinationMarker, currentLocationMarker},
polylines: Set<Polyline>.of(polylines.values),
initialCameraPosition: CameraPosition(
target: currentLocation!,
zoom: 16.0,
),
),
)
],
),
)
);
}
}
Current Output : https://phpout.com/wp-content/uploads/2023/06/YYaJb-jpg.webp
The name of locations are hidden due to containing my current location.
How do I place the markers outside of widget build and then make the polyline between markers available?
2
Answers
Go check Flutter Campus, there’s something you want.
The issue was happening cause the map renders on the screen but the polyLines are still not generated.
You need to use cubit, that manage all those async gaps for get current location and finding polyLines.
you can refer to the the below code:
MapsCubit.dart:
MapsSate.dart:
And Maps.dart:
Now the cubit methods getCurrentLocation and getPolyLines will manage the async operation of the getting current location and generating polyLines respectively and will emit a state, and bloc consumer will catch it and will show google maps.