title: Flutter - Get location from another user and show it on a map for the customer user
body: <p>I'm devoloping an uber like app that I need to show the &quot;driver&quot; location to the user and track it in real time, I was able to track the location but just from the user and I'm out of ideias, tried watching some videos and getting some codes but its my first flutter app.
  Here's the current code that I'm using to get the user location but trying to get the &quot;driver&quot;.</p>
  <pre><code>import 'dart:async';
  
  import 'package:flutter/material.dart';
  import 'package:flutter_polyline_points/flutter_polyline_points.dart';
  import 'package:google_maps_flutter/google_maps_flutter.dart';
  import 'package:hapdog/constants.dart';
  import 'package:location/location.dart';
  import 'package:hapdog/services/google_map_api.dart';
  
  class MapScreen extends StatefulWidget {
    const MapScreen({Key? key}) : super(key: key);
    static const String id = 'map_screen';
  
    @override
    _MapScreenState createState() =&gt; _MapScreenState();
  }
  
  class _MapScreenState extends State&lt;MapScreen&gt; {
    late BitmapDescriptor pinLocationIcon;
  
    LatLng sourceLocation = LatLng(28.432864, 77.002563);
    LatLng destinationLatlng = LatLng(28.431626, 77.002475);
  
    Completer&lt;GoogleMapController&gt; _controller = Completer();
  
    Set&lt;Marker&gt; _marker = Set&lt;Marker&gt;();
  
    Set&lt;Polyline&gt; _polylines = Set&lt;Polyline&gt;();
    List&lt;LatLng&gt; polylineCoordinates = [];
    late PolylinePoints polylinePoints;
  
    late StreamSubscription&lt;LocationData&gt; subscription;
  
    LocationData? currentLocation;
    late LocationData destinationLocation;
    late Location location;
  
    @override
    void initState() {
      super.initState();
  
      BitmapDescriptor.fromAssetImage(
              const ImageConfiguration(), 'assets/dog_icon.png')
          .then((onValue) {
        pinLocationIcon = onValue;
      });
  
      location = Location();
      polylinePoints = PolylinePoints();
  
      subscription = location.onLocationChanged.listen((clocation) {
        currentLocation = clocation;
  
        updatePinsOnMap();
      });
  
      setInitialLocation();
    }
  
    void setInitialLocation() async {
      await location.getLocation().then((value) {
        currentLocation = value;
        setState(() {});
      });
      destinationLatlng = LatLng(
          currentLocation!.latitude ?? 0.0, currentLocation!.longitude ?? 0.0);
  
      destinationLocation = LocationData.fromMap({
        &quot;latitude&quot;: destinationLatlng.latitude,
        &quot;longitude&quot;: destinationLatlng.longitude,
      });
    }
  
    void showLocationPins() {
      var sourceposition = LatLng(
          currentLocation!.latitude ?? 0.0, currentLocation!.longitude ?? 0.0);
  
      var destinationPosition =
          LatLng(destinationLatlng.latitude, destinationLatlng.longitude);
  
      // _marker.add(Marker(
      //   icon: pinLocationIcon,
      //   markerId: MarkerId('sourcePosition'),
      //   position: sourceposition,
      // ));
  
      // _marker.add(
      //   Marker(
      //     icon: pinLocationIcon,
      //     markerId: MarkerId('destinationPosition'),
      //     position: destinationPosition,
      //   ),
      // );
    }
  
    void setPolylines() async {
      PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
          kGoogleAPIKey,
          PointLatLng(currentLocation!.latitude ?? 0.0,
              currentLocation!.longitude ?? 0.0),
          PointLatLng(destinationLatlng.latitude, destinationLatlng.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));
        });
      }
    }
  
    void updatePinsOnMap() async {
      CameraPosition cameraPosition = CameraPosition(
        zoom: 19,
        bearing: 30,
        target: LatLng(
            currentLocation!.latitude ?? 0.0, currentLocation!.longitude ?? 0.0),
      );
  
      final GoogleMapController controller = await _controller.future;
  
      controller.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
  
      var sourcePosition = LatLng(
          currentLocation!.latitude ?? 0.0, currentLocation!.longitude ?? 0.0);
  
      setState(() {
        _marker.removeWhere((marker) =&gt; marker.mapsId.value == 'sourcePosition');
  
        _marker.add(Marker(
          icon: pinLocationIcon,
          markerId: MarkerId('sourcePosition'),
          position: sourcePosition,
        ));
      });
    }
  
    @override
    Widget build(BuildContext context) {
      CameraPosition initialCameraPosition = CameraPosition(
        zoom: 15,
        bearing: 30,
        target: currentLocation != null
            ? LatLng(currentLocation!.latitude ?? 0.0,
                currentLocation!.longitude ?? 0.0)
            : LatLng(0.0, 0.0),
      );
  
      return currentLocation == null
          ? Container(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              alignment: Alignment.center,
              child: CircularProgressIndicator(),
            )
          : SafeArea(
              child: Scaffold(
                body: GoogleMap(
                  myLocationButtonEnabled: true,
                  compassEnabled: true,
                  markers: _marker,
                  polylines: _polylines,
                  mapType: MapType.normal,
                  initialCameraPosition: initialCameraPosition,
                  onMapCreated: (GoogleMapController controller) {
                    _controller.complete(controller);
                    showLocationPins();
                    setPolylines();
                  },
                ),
              ),
            );
    }
  
    @override
    void dispose() {
      subscription.cancel();
      super.dispose();
    }
  }
  </code></pre>