Home > database >  Flutter & Google Maps, Tracking by getting latitude and longitude from firebase
Flutter & Google Maps, Tracking by getting latitude and longitude from firebase

Time:06-08

I want to make an application with flutter that gets the location data(latitude and longitude) and show it on the map,

The only problem I have, is I don't know how to give the location data to the google map to show it, I'm new to the flutter, and I searched online and didn't get any answer if anyone can guide me or something on how I can give the location data to the google map, I will be grateful.

CodePudding user response:

Set up an onSnapshot - type listener on a collection that updates (however often you want) your latitude and longitude in a document on the front end (Flutter).

Helpful hint might be:

https://firebase.google.com/docs/firestore/query-data/listen#dart

final docRef = db.collection("locations").doc('User Docs);
docRef.snapshots().listen(
      // On the line below you can do whatever you'd like with the return info "event.data()"
      (event) => print("current data: ${event.data()}"),
      one rror: (error) => print("Listen failed: $error"),
    );

CodePudding user response:

import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:permission_handler/permission_handler.dart';    

///initializing   
 Set<Marker> markers = {};
        LatLng? startLocation;
        GoogleMapController? mapController;
         bool loader = false;
    
        ///current location permission
    
          determinePosition(pr) async {
            bool serviceEnabled;
            loader = true;
            serviceEnabled = await Geolocator.isLocationServiceEnabled();
        
            if (serviceEnabled) {
              
           getPlace();
             
            }
          }
        
        ///getting current location and setting start location
    
        void getPlace() async {
            Map<Permission, PermissionStatus> statuses = await [
              Permission.location,
            ].request();
            if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
              print('no');
            }
            Position position = await Geolocator.getCurrentPosition(
                desiredAccuracy: LocationAccuracy.high);
            startLocation = LatLng(position.latitude, position.longitude);
        
            addMarkers();
          }
        
        
     
    
          ///adding markers to googlemap
    
        addMarkers() async {
           
        
            markers.add(
                Marker(
                  markerId:MarkerId('SomeId'),
                  position: LatLng(
                    double.parse(homeData!.data!.items![i].latitude),
                    double.parse(homeData!.data!.items![i].longitude),
                  ),
                  infoWindow: InfoWindow(
                    //popup info
                    title: '',
                    snippet: '',
                  ),
                ),
              );
            }
            loader = false;
           setState((){});
          }
        
    ///map widget 
        
  

     Widget getMap(){return    loader?   GoogleMap(
                                  zoomGesturesEnabled: true,
                                  initialCameraPosition: CameraPosition(
                                    target: control.startLocation ??
                                        LatLng(24.874594, 67.075709), //initial position
                                    zoom: 30, //initial zoom level
                                  ),
                                  markers: control.markers, //markers to show on map
                                  mapType: MapType.normal, //map type
                                  onMapCreated: (controller) {
                                    //method called when map is created
                                    setState(() {
                                      control.mapController = controller;
                                    });
                                  },
                                ):CircleProgressIndecator(),}

here is some work of how to show your current location on googlemap hope it will help you
  • Related