Search code examples
flutterdartgeolocator

Can anyone tell me how to add the geolocator listen position to a google marker?


I am trying to make a navigation marker on google map, for the user location I am using geolocator pub dev with the listen function however it returned as a Position? and since it have the posibility of null, it can't be used directly can anyone tell me how to do it ?

Position? listenToLocation()
  {
    Position? userposition;
    final LocationSettings _locationSettings = LocationSettings(accuracy: LocationAccuracy.high,distanceFilter: 100);
    userStreamlocation = Geolocator.getPositionStream(locationSettings: _locationSettings).listen(
    (userposition) {
        print(userposition == null ? 'Unknown' : '${userposition.latitude.toString()}, ${userposition.longitude.toString()}');
        setState(){
          streamResult=userposition;
        }
    });
    return userposition;
  }

Solution

  • I am able to fix this after finding out that the marker didn't got updated because it is in a different state than the main widget, I fix it trough the use of the statebuilder widget, here's the code snip

    StatefulBuilder(
            builder: (context, setMapState) {
              setMapState(
                () {},
              );
    
              void _whenMapCreated(GoogleMapController controller) async {
                //debugPrint('>>>>>>>>> onMapCreated');
                mapController = controller;
                _Navmarkers.clear();
                _navPolylines.clear();
                var direction =
                    await LocationService().getDirectionNoWP(userOrigin, userGoal);
                _setPolylines(direction['polyline_decoded']);
                setMapState(
                  () {},
                );
              }
    
              return Column(children: [
                Expanded(
                    child: GoogleMap(
                  mapType: MapType.normal,
                  initialCameraPosition:
                      CameraPosition(target: _kGooglePlex, zoom: 11),
                  markers: _Navmarkers,
                  polylines: _navPolylines,
                  onMapCreated: _whenMapCreated,
                )),
                Row(
                  children: [
                    ButtonBar(
                      children: [
                        IconButton(
                            onPressed: () {
                              Position? userposition;
    
                              const LocationSettings _locationSettings =
                                  LocationSettings(
                                accuracy: LocationAccuracy.high,
                                distanceFilter: 100,
                              );
                              userStreamlocation = Geolocator.getPositionStream(
                                      locationSettings: _locationSettings)
                                  .listen((userposition) {
                                _Navmarkers.clear();
                                _Navmarkers.add(Marker(
                                    markerId: MarkerId('User'),
                                    position: LatLng(userposition.latitude,
                                        userposition.longitude),
                                    icon: BitmapDescriptor.defaultMarkerWithHue(
                                        BitmapDescriptor.hueRose)));
                                setMapState(() {                            
                                });                            
                              });
                            },
                            icon: Icon(Icons.navigation)),
                        IconButton(
                            onPressed: () {
                              setMapState(
                                () {                             
                                },
                              );
                            },
                            icon: Icon(Icons.refresh)),
                        IconButton(
                            onPressed: () {
                              dispose();
                            },
                            icon: Icon(Icons.stop))
                      ],
                    )
                  ],
                )
              ]);
            },
          ),
    

    This way the googlemap have its own state and can be refreshed