title: How to draw polyline in google maps flutter between user location and a marker?
body: <p>This is my first post in this site so I hope I get the help I need.</p>
  <p>I'm new to flutter and I'm trying to use flutter_polyline_points plugin to draw polylines or routes between the user location and a marker that will be specified by the user. I searched online and I went over many tutorials but I kept getting errors or it didn't work with my codes.</p>
  <p>So can someone please help me with achieving that?</p>
  <p>Thanks in advance!</p>
  <p>These are my codes:</p>
  <pre><code>const LatLng dest_location = LatLng(26.0667, 50.5577);
  
  class _NavPageScreenState extends State&lt;NavPage&gt; {
    Completer&lt;GoogleMapController&gt; _controllerGoogleMap = Completer();
    late GoogleMapController newGoogleMapController;
    late Position currentPosition;
    var geoLocator = Geolocator();
    Map&lt;MarkerId, Marker&gt; markers = {};
    Map&lt;PolylineId, Polyline&gt; polylines = {};
    List&lt;LatLng&gt; polylineCoordinates = [];
    PolylinePoints polylinePoints = PolylinePoints();
  
    void locatePosition() async {
      Position position = await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.high);
      currentPosition = position;
      LatLng latlatposition = LatLng(position.latitude, position.longitude);
      CameraPosition cameraPosition =
          new CameraPosition(target: latlatposition, zoom: 16);
      newGoogleMapController
          .animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
    }
  
    static final CameraPosition _UserLocation = CameraPosition(
      target: LatLng(26.0667, 50.5577),
      zoom: 16,
    );
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Google Maps'),
          backgroundColor: Colors.redAccent,
          leading: new IconButton(
              icon: new Icon(Icons.arrow_back),
              onPressed: () {
                Navigator.push(
                    context, MaterialPageRoute(builder: (context) =&gt; HomePage()));
              }),
        ),
        body: GoogleMap(
          mapType: MapType.normal,
          myLocationButtonEnabled: true,
          initialCameraPosition: _UserLocation,
          polylines: Set&lt;Polyline&gt;.of(polylines.values),
          markers: Set&lt;Marker&gt;.of(markers.values),
          myLocationEnabled: true,
          zoomGesturesEnabled: true,
          zoomControlsEnabled: true,
          onMapCreated: (GoogleMapController controller) {
            _controllerGoogleMap.complete(controller);
            newGoogleMapController = controller;
            _getPolyline();
            locatePosition();
            setState(() {});
          },
        ),
      );
    }
  
    _addMarker(LatLng position, String id, BitmapDescriptor descriptor) {
      MarkerId markerId = MarkerId(id);
      Marker marker =
          Marker(markerId: markerId, icon: descriptor, position: position);
      markers[markerId] = marker;
    }
  
    _addPolyLine(List&lt;LatLng&gt; polylineCoordinates) {
      PolylineId id = PolylineId(&quot;poly&quot;);
      Polyline polyline = Polyline(
        polylineId: id,
        points: polylineCoordinates,
        width: 8,
      );
      polylines[id] = polyline;
      setState(() {});
    }
  
    void _getPolyline() async {
      /// add origin marker origin marker
      _addMarker(
        LatLng(currentPosition.latitude, currentPosition.longitude),
        &quot;origin&quot;,
        BitmapDescriptor.defaultMarker,
      );
  
      // Add destination marker
      _addMarker(
        LatLng(dest_location.latitude, dest_location.longitude),
        &quot;destination&quot;,
        BitmapDescriptor.defaultMarkerWithHue(90),
      );
  
      List&lt;LatLng&gt; polylineCoordinates = [];
  
      PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
        &quot;API_KEY&quot;,
        PointLatLng(currentPosition.latitude, currentPosition.longitude),
        PointLatLng(dest_location.latitude, dest_location.longitude),
        travelMode: TravelMode.walking,
      );
      if (result.points.isNotEmpty) {
        result.points.forEach((PointLatLng point) {
          polylineCoordinates.add(LatLng(point.latitude, point.longitude));
        });
      } else {
        print(result.errorMessage);
      }
      _addPolyLine(polylineCoordinates);
    }
  }
  </code></pre>