Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload My Location button when permission is granted

Background

I am using the google_maps_flutter package to show a map with markers corresponding to houses. To avoid overloading devices, I only load houses near the location the user is looking at. To make this easier, I request the user's location and provide that to Google Maps in order to show a My Location button.

The Problem

If location access has already been granted, it works as intended. Whoever if the map is already loaded, the My Location button does not show up.

Currently, if location permission is granted while the map is on-screen, I simply move the camera over to their location. This is an OK workaround, but I'd rather let the user decide where to look.

How can I load in the button when permission is granted while the map is already loaded?

Tried

  1. I have tried just setting state.
  2. Manually marking the map as dirty.
  3. Forcing the map to reload options using a test method.

I've checked every available method I had access to, so at this point I doubt there is any other option. If this is the case I will report it as a bug.

My Code

  @override
  Widget build(BuildContext context) {
    // Load markers when permission is granted.
    location.hasPermission().then((currPerm) async {
      if (!currPerm) {
        bool newPerm = await location.requestPermission();
        if (newPerm) {
          LocationData data = await location.getLocation();
          (await mapController.future).moveCamera(CameraUpdate.newLatLng(LatLng(data.latitude, data.longitude)));
        }
      }
    });

    GoogleMap map = GoogleMap(
      initialCameraPosition: CameraPosition(
        target: INIT_POS,
        zoom: 11.5,
      ),
      myLocationEnabled: true,
      myLocationButtonEnabled: true,
      trafficEnabled: true,
      markers: markers,
      onMapCreated: (newController) => mapController.complete(newController),
      onCameraIdle: () async {
        // Load markers when the camera stops moving.
        LatLngBounds bounds = await (await mapController.future).getVisibleRegion();
        loadMarkers(
          LatLng(
            (bounds.northeast.latitude + bounds.southwest.latitude)/2,
            (bounds.northeast.longitude + bounds.southwest.longitude)/2
          ),
        );
      },
    );
    return map;
  }
like image 910
cj- Avatar asked Oct 28 '25 10:10

cj-


1 Answers

As of google_maps_flutter: ^0.5.23+1, I solved it by assigning a variable to myLocationEnabled, and changing its value once I got the location permission. If you hardcode true, it just won't work.

GoogleMap map = GoogleMap(
      myLocationEnabled: _isLocationGranted,
      ...

So, _isLocationGranted is initially false. When I get location permission, I set it to true and rebuild the widget (by calling setState(), triggering BlocBuilder(), or whatever method you use).

The other related parameter, myLocationButtonEnabled: true, doesn't seem to need this handling.

like image 95
maganap Avatar answered Oct 30 '25 03:10

maganap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!