title: flutter dart language code get current location of user and extract last firebase location and calculate remaining time and distance draw poly lines
body: <p>i already make a flutter dart language through which i update my location to firebase i want make one more dart funtion on mymap.dart file which get current location of user and extract last firebase location and calculate remaining time and distance in widget and on map draw poly lines thanks
  Here is my complete code with code you will understand all working of app</p>
  <pre class="lang-dart prettyprint-override"><code>import 'dart:async';
  import 'package:cloud_firestore/cloud_firestore.dart';
  import 'package:firebase_auth/firebase_auth.dart';
  import 'package:firebase_core/firebase_core.dart';
  import 'package:flutter/material.dart';
  import 'package:welcome_login/Login.dart';
  import 'ui/mymap.dart';
  import 'getstdloc.dart';
  import 'package:location/location.dart' as loc;
  
  void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    runApp(MaterialApp(home: GPSscreen()));
  }
  
  class GPSscreen extends StatefulWidget {
    const GPSscreen({
      Key? key,
    }) : super(key: key);
    @override
    _GPSscreenState createState() =&gt; _GPSscreenState();
  }
  
  class _GPSscreenState extends State&lt;GPSscreen&gt; {
    final loc.Location location = loc.Location();
    StreamSubscription&lt;loc.LocationData&gt;? _locationSubscription;
  
    @override
    void initState() {
      super.initState();
      location.changeSettings(interval: 300, accuracy: loc.LocationAccuracy.high);
      location.enableBackgroundMode(enable: true);
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Live UOH Buses Locations'),
        ),
        body: Column(
          children: [
            TextButton(
                onPressed: () {
                  _getLocation();
                },
                child: Text('add my location')),
            TextButton(
                onPressed: () {
                  _listenLocation();
                },
                child: Text('enable live location')),
            TextButton(
                onPressed: () {
                  _stopListening();
                },
                child: Text('stop live location')),
            Expanded(
                child: StreamBuilder(
              stream:
                  FirebaseFirestore.instance.collection('location').snapshots(),
              builder: (context, AsyncSnapshot&lt;QuerySnapshot&gt; snapshot) {
                if (!snapshot.hasData) {
                  return Center(child: CircularProgressIndicator());
                }
                return ListView.builder(
                    itemCount: snapshot.data?.docs.length,
                    itemBuilder: (context, index) {
                      return ListTile(
                        title:
                            Text(snapshot.data!.docs[index]['name'].toString()),
                        subtitle: Row(
                          children: [
                            Text(snapshot.data!.docs[index]['latitude']
                                .toString()),
                            SizedBox(
                              width: 20,
                            ),
                            Text(snapshot.data!.docs[index]['longitude']
                                .toString()),
                          ],
                        ),
                        trailing: IconButton(
                          icon: Icon(Icons.directions),
                          onPressed: () {
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (context) =&gt;
                                    MyMap(snapshot.data!.docs[index].id)));
                          },
                        ),
                      );
                    });
              },
            )),
          ],
        ),
      );
    }
  
    _getLocation() async {
      try {
        final loc.LocationData _locationResult = await location.getLocation();
        await FirebaseFirestore.instance.collection('location').doc('user1').set({
          'latitude': _locationResult.latitude,
          'longitude': _locationResult.longitude,
          'name': 'bus 1'
        }, SetOptions(merge: true));
      } catch (e) {
        print(e);
      }
    }
  
    Future&lt;void&gt; _listenLocation() async {
      _locationSubscription = location.onLocationChanged.handleError((onError) {
        print(onError);
        _locationSubscription?.cancel();
        setState(() {
          _locationSubscription = null;
        });
      }).listen((loc.LocationData currentlocation) async {
        await FirebaseFirestore.instance.collection('location').doc('user1').set({
          'latitude': currentlocation.latitude,
          'longitude': currentlocation.longitude,
          'name': User,
        }, SetOptions(merge: true));
      });
    }
  
    _stopListening() {
      _locationSubscription?.cancel();
      setState(() {
        _locationSubscription = null;
      });
    }
  }
  
  class MyMap extends StatefulWidget {
    final String user_id;
    MyMap(this.user_id);
    @override
    _MyMapState createState() =&gt; _MyMapState();
  }
  
  class _MyMapState extends State&lt;MyMap&gt; {
    final loc.Location location = loc.Location();
    late GoogleMapController _controller;
    bool _added = false;
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          appBar: AppBar(
            title: Text('Live UOH Buses Locations'),
          ),
          body: StreamBuilder(
            stream: FirebaseFirestore.instance.collection('location').snapshots(),
            builder: (context, AsyncSnapshot&lt;QuerySnapshot&gt; snapshot) {
              if (_added) {
                mymap(snapshot);
              }
              if (!snapshot.hasData) {
                return Center(child: CircularProgressIndicator());
              }
  
              return GoogleMap(
                mapType: MapType.normal,
                markers: {
                  Marker(
                      position: LatLng(
                        snapshot.data!.docs.singleWhere((element) =&gt;
                            element.id == widget.user_id)['latitude'],
                        snapshot.data!.docs.singleWhere((element) =&gt;
                            element.id == widget.user_id)['longitude'],
                      ),
                      markerId: MarkerId('id'),
                      icon: BitmapDescriptor.defaultMarker),
                },
                initialCameraPosition: CameraPosition(
                    target: LatLng(
                      snapshot.data!.docs.singleWhere(
                          (element) =&gt; element.id == widget.user_id)['latitude'],
                      snapshot.data!.docs.singleWhere(
                          (element) =&gt; element.id == widget.user_id)['longitude'],
                    ),
                    zoom: 14.47),
                onMapCreated: (GoogleMapController controller) async {
                  setState(() {
                    _controller = controller;
                    _added = true;
                  });
                },
              );
            },
          ));
    }
  
    Future&lt;void&gt; mymap(AsyncSnapshot&lt;QuerySnapshot&gt; snapshot) async {
      await _controller
          .animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
              target: LatLng(
                snapshot.data!.docs.singleWhere(
                    (element) =&gt; element.id == widget.user_id)['latitude'],
                snapshot.data!.docs.singleWhere(
                    (element) =&gt; element.id == widget.user_id)['longitude'],
              ),
              zoom: 14.47)));
    }
  }
  </code></pre>