Google map takes a lot of time to load with current location in Flutter
3,474
Seems like this is a known issue. Check this Git Issue link. As suggested in the last comment, as a workaround you can create a FutureBuilder
with a simulated delay as the future and then use a progress indicator to show the loading.
Comments
-
Newaj over 1 year
I am trying to open google map with current location. For this, I am using a stateful widget. But map takes a lot of time to load, sometimes cant even load at all. Is this becuase of a
Future
function(for getting current location) or my implementation is wrong? I am using google_maps_flutter plugin for map. Here is my code :@override Widget build(BuildContext context) { return Scaffold( body: Stack( children: <Widget>[ FutureBuilder( future: _getCurrentLocation(), builder: (context, snapshot) { if (snapshot.hasData) { print("map loaded"); Placemark placemark = snapshot.data; var json = placemark.toJson(); selectedLocation = TheLocation.fromJson(json); _markers.add( Marker( markerId: MarkerId('homeMarker'), position: LatLng( selectedLocation.latitude, selectedLocation.longitude), icon: BitmapDescriptor.defaultMarker, infoWindow: InfoWindow(title: selectedLocation.name), draggable: false, ), ); return GoogleMap( initialCameraPosition: CameraPosition( target: LatLng(placemark.position.latitude, placemark.position.longitude), zoom: 18, ), markers: _markers, onMapCreated: onMapCreated, onCameraMove: ((_position) => _updatePosition(_position)), ); } else { print("loading map"); return Container( child: Center( child: Text("Loading map. Please Wait ... ..."), ), ); } }, ), ], ), ); }
Using geolocator plugin for getting current location :
Future<Placemark> _getCurrentLocation() async { List<Placemark> placeMarks; await geolocator .getCurrentPosition(desiredAccuracy: LocationAccuracy.best) .then((Position position) async { placeMarks = await Geolocator().placemarkFromCoordinates( position.latitude, position.longitude, localeIdentifier: "en_US", ); }).catchError((e) { print("Location error $e"); }); return placeMarks[0]; }
onMapCreated
function :void onMapCreated(GoogleMapController controller) { setState(() { googleMapController = controller; }); }