Melik BOZKURT
Melik BOZKURT

Reputation: 190

Flutter Google Maps doesn't show location on first launch

I'm using Google Maps (google_maps_flutter: ^2.1.3) in my Flutter app, and when I open the app on the phone for the first time, it asks for location permission. After giving this permission, my location on the Map is not shown with a blue dot and the go to my location button is not visible either. However, after that, when I close the application and open it again, both my location appears with a blue dot and the go to my location button is actively displayed on the screen.

Here is my GoogleMap widget:

GoogleMap(     
                          myLocationEnabled: true,
                          myLocationButtonEnabled: true,
                          mapType: MapType.normal,
                          initialCameraPosition: const CameraPosition(
                            target: LatLng(30, 30),
                            zoom: 4,
                          ),
                          markers: markers,
                          mapToolbarEnabled: false,
                        ),

What do I need to do in order for my location to appear on Google Maps after I give location permission when I first open the application?

Upvotes: 1

Views: 3176

Answers (2)

Melik BOZKURT
Melik BOZKURT

Reputation: 190

I was initially launching my app from HomePage but to fix this issue I created the transition page LandingPage. In LandingPage, I first checked if the user's location is turned on. I forced the user to turn on their location if it wasn't turned on. If it's turned on, I got the user's location and sent that location to HomePage using constructor. In HomePage, I started the map from this location. Thus, the user gave the location permission on the previous transition page and I used the location we got on that page as the map starting location.

I performed the following operations on LandingPage:

bool isLocationEnabled = false;
  bool isLocationEnabledChecked = false;
  double? latMe;
  double? longMe;
  Location _locationTracker = Location();

  Future<void> checkIsLocationEnabled() async {
    isLocationEnabled = await _locationTracker.serviceEnabled();
    setState(() {});
    if (isLocationEnabled) {
      var location = await _locationTracker.getLocation();
      latMe = location.latitude;
      longMe = location.longitude;
    }
    setState(() {
      isLocationEnabledChecked = true;
    });
  }


@override
  void initState() {
    super.initState();
    checkIsLocationEnabled();
  } 

@override
  Widget build(BuildContext context) {
      if (isLocationEnabledChecked) {
        if (isLocationEnabled) {
          return HomePage(
            latitude: latMe,
            longitude: longMe,
          );
        } else {
          return EnableLocationScreen();
        }
      } else {
        return WaitingScreen();
      }
  }

I performed the following operations on HomePage:

GoogleMap(
                                onMapCreated: (GoogleMapController controller) async {
                                  _customInfoWindowController.googleMapController = controller;
                                  mapController.complete(controller);
                                },
                                onTap: (position) {
                                  _customInfoWindowController.hideInfoWindow!();
                                },
                                onCameraMove: (position) {
                                  _customInfoWindowController.onCameraMove!();
                                },
                                myLocationEnabled: true,
                                myLocationButtonEnabled: true,
                                mapType: MapType.normal,
                                initialCameraPosition: CameraPosition(
                                  target: LatLng(widget.latitude ?? 38.917001095328345, widget.longitude ?? 35.532376170158386),
                                  zoom: 15,
                                ),
                                markers: markers,
                                polygons: polygon,
                                mapToolbarEnabled: false,
                                minMaxZoomPreference: const MinMaxZoomPreference(10, 20),
                              ),

I am using location: ^4.3.0 package.

Upvotes: 1

Abu Qudama
Abu Qudama

Reputation: 65

In IOS it works and in android it needs reboot so show permission on splash before opening map page or after getting permission reload page. Simple solution set bool isLocationEnabled=false; then when you get permission from user make

setState(() {
      isLocationEnabled=true;
    }); 

and set the value as follows

GoogleMap(     
                      myLocationEnabled: isLocationEnabled,
                      myLocationButtonEnabled: true,
                      mapType: MapType.normal,
                      initialCameraPosition: const CameraPosition(
                        target: LatLng(30, 30),
                        zoom: 4,
                      ),
                      markers: markers,
                      mapToolbarEnabled: false,
                    ),

Upvotes: 1

Related Questions