Jockey
Jockey

Reputation: 1

Problem with location and google maps on flutter

I have a problem with location and google maps in flutter: I'm building an app whom first screen is a map from Google and I'm trying to use user location as "initialCameraPosition" of the map. The problem is that when I start the application when the location already on, everything is okay, I see the map clearly and also the user location is okay. Instead, when I start the application with the location off, even though I able the location when the app starts then I cannot see the map anymore and it seems that the location is null.

Here's my code (map screen):

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';
import 'package:stabia_go/location.dart';

import 'package:stabia_go/markers.dart';

class GoogleMapScreen extends StatefulWidget {
  @override
  _GoogleMapScreenState createState() => _GoogleMapScreenState();
}

class _GoogleMapScreenState extends State<GoogleMapScreen> {
  Set<Marker> _markers = {};

  void _onMapCreated(GoogleMapController controller) {
    setState(() {
      _markers.addAll(markers);
    });
  }

  @override
  void initState() {
    super.initState();
    Provider.of<LocationProvider>(context, listen: false).initialization();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Percorso Suggerito'),
      ),
      body: googleMapUI(),
    );
  }

  Widget googleMapUI() {
    return Consumer<LocationProvider>(builder: (consumerContext, model, child) {
      if (model.locationPosition != null) {
        return GoogleMap(
          markers: _markers,
          initialCameraPosition:
              CameraPosition(target: model.locationPosition, zoom: 15),
          myLocationEnabled: true,
          myLocationButtonEnabled: true,
          onMapCreated: _onMapCreated,
        );
      }

      return Container(
        child: Center(
          child: CircularProgressIndicator(),
        ),
      );
    });
  }
}

Here's the code of the location file:

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';

class LocationProvider with ChangeNotifier {
  Location _location;
  Location get location => _location;

  LatLng _locationPosition;
  LatLng get locationPosition => _locationPosition;

  LocationProvider() {
    _location = new Location();
  }

  initialization() async {
    await getUserLocation();
  }

  getUserLocation() async {
    bool serviceEnabled;
    PermissionStatus permissionGranted;

    serviceEnabled = await location.serviceEnabled();
    if (!serviceEnabled) {
      serviceEnabled = await location.requestService();
      if (!serviceEnabled) {
        return;
      }
    }

    permissionGranted = await location.hasPermission();
    if (permissionGranted == PermissionStatus.denied) {
      permissionGranted = await location.requestPermission();
      if (permissionGranted != PermissionStatus.granted) {
        return;
      }
    }
    location.onLocationChanged.listen(
      (LocationData currentLocation) {
        _locationPosition = LatLng(
          currentLocation.latitude,
          currentLocation.longitude,
        );

        print(_locationPosition);
        notifyListeners();
      },
    );
  }
}

Where's my mistake? I'm using Location, Provider and Google Maps flutter packages

Upvotes: 0

Views: 1543

Answers (1)

Prabhanshu Tiwari
Prabhanshu Tiwari

Reputation: 287

Try to check for location access when your app is launching. if permission is denied the redirect user to allow location access. if user allow then everything will work fine else user deny access then show a hard coded location.

Upvotes: 0

Related Questions