Flutter - Google Maps doesn´t wait to Location

后端 未结 3 1987
不思量自难忘°
不思量自难忘° 2021-01-20 16:16

I´m trying to show a map with current location. For do this i used Location and Google Map plugins (last version).

I have a code similar to:

var lng,         


        
相关标签:
3条回答
  • 2021-01-20 16:19

    you can display a loader until you get your location,

    var lng, lat;
    
          @override
          initState() {
            super.initState();
            loading = true;
            getLocation();
          }
    
              Future getLocation() async {
                    final location = Location();
                    var currentLocation = await location.getLocation();
                    setState(() {
                      lat = currentLocation.latitude;
                      lng = currentLocation.longitude;
                      loading=false;
                    });
                  }
    
             loading==false ? GoogleMap(
                 mapType: MapType.hybrid,
                 myLocationButtonEnabled: true,
                 myLocationEnabled: true,
                 initialCameraPosition: CameraPosition(
                      target: LatLng(lat, lng),
                      zoom: 15.0,
                            )):CircularProgressIndicator(),
    
    0 讨论(0)
  • 2021-01-20 16:20

    Show an empty Container() or any loading indicator while your lat and lng is null.

    lat == null || lng == null
      ? Container()
      : GoogleMap(
          mapType: MapType.hybrid,
          myLocationButtonEnabled: true,
          myLocationEnabled: true,
          initialCameraPosition: CameraPosition(
            target: LatLng(lat, lng),
            zoom: 15.0,
          ),
        );
    
    0 讨论(0)
  • 2021-01-20 16:32

    You can also try this, and get rid of the empty Container.

    GoogleMap(
          mapType: MapType.hybrid,
          myLocationButtonEnabled: true,
          myLocationEnabled: true,
          initialCameraPosition: CameraPosition(
            target: LatLng(lat ?? 0, lng ?? 0), // so if lat, lng is null, we use 0 as placeholder. 
            zoom: 15.0,
          ),
        )
    
    0 讨论(0)
提交回复
热议问题