import 'dart:async'; import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:flutter_polyline_points/flutter_polyline_points.dart'; import 'package:get/get.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'location_controller.dart'; class OrderTrackingPage extends StatefulWidget { var lat; var lng; OrderTrackingPage({ this.lat,this.lng }); @override OrderTrackingPageState createState() => OrderTrackingPageState(); } class OrderTrackingPageState extends State { final Completer mapController = Completer(); PolylinePoints polylinePoints = PolylinePoints(); double latitude=0; double longitude=0; String googleAPiKey = "AIzaSyBOigf-qg4v_aD0Jrx2wFOMNzObxXrfDEM"; Set markers = {}; Map polylines = {}; LatLng startLocation = const LatLng(0,0); LocationController locationController = Get.put(LocationController()); @override void initState() { super.initState(); latitude=widget.lat; longitude=widget.lng; LatLng endLocation = LatLng(widget.lat,widget.lng); ever(locationController.locationPosition, (value) { if (value != null) { log("${value.latitude} ${value.longitude}"); var latitude = value.latitude; var longitude = value.longitude; startLocation = LatLng(latitude, longitude); getDirections(endLocation); } }); getDirections(endLocation); //fetch direction polylines from Google API } getDirections(endLocation) async { markers.clear(); markers.add(Marker( markerId: MarkerId(startLocation.toString()), position: startLocation, infoWindow: const InfoWindow( title: 'Starting Point ', snippet: 'Start Marker', ), icon: locationController.pickupMarker ?? BitmapDescriptor.defaultMarker, )); markers.add(Marker( markerId: MarkerId(endLocation.toString()), position: endLocation, //position of marker infoWindow: const InfoWindow( title: 'Destination Point ', snippet: 'Destination Marker', ), icon: locationController.dropMarker ?? BitmapDescriptor.defaultMarker, )); List polylineCoordinates = []; PolylineResult result = await polylinePoints.getRouteBetweenCoordinates( googleAPiKey, PointLatLng(startLocation.latitude, startLocation.longitude), PointLatLng(endLocation.latitude, endLocation.longitude), travelMode: TravelMode.driving, ); if (result.points.isNotEmpty) { for (var point in result.points) { polylineCoordinates.add(LatLng(point.latitude, point.longitude)); } } else { log(result.errorMessage ?? "Something went wrong"); } addPolyLine(polylineCoordinates); } addPolyLine(List polylineCoordinates) async { PolylineId id = const PolylineId("poly"); Polyline polyline = Polyline( polylineId: id, color: Colors.blueAccent, points: polylineCoordinates, width: 8, ); polylines[id] = polyline; var position = CameraPosition( target: LatLng(startLocation.latitude, startLocation.longitude), zoom: 21); final GoogleMapController controller = await mapController.future; controller.animateCamera(CameraUpdate.newCameraPosition(position)); setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( body: GoogleMap( //Map widget from google_maps_flutter package zoomGesturesEnabled: true, //enable Zoom in, out on map initialCameraPosition: CameraPosition( //innital position in map target: startLocation, //initial position zoom: 5.0, //initial zoom level ), markers: markers, //markers to show on map polylines: Set.of(polylines.values), //polylines mapType: MapType.normal, //map type onMapCreated: (controller) { //method called when map is created if (!mapController.isCompleted) { mapController.complete(controller); } }, ), ); } }