DEV Community

loading...

Current Location Google Maps Flutter

rrtutors profile image rrtutors Originally published at rrtutors.com Updated on ・1 min read

Display the Location information in the mobile applications is Mandatory functionality. User will get the more information form the location sharing.

How to fetch the Current Location in Flutter Application? In this Post we will cover show the current Location on Google Maps

Alt Text

To do this we will use the google_maps_plugin .

Step 1: Create Flutter Application Step 2: Add plugins in pubspec.yaml file

dev_dependencies:
   google_maps_flutter:
Enter fullscreen mode Exit fullscreen mode

Step 3: Update Manifest file with Request Permissions (Android)

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Enter fullscreen mode Exit fullscreen mode

Step 4: Add Google Maps This Google Maps widget will Load the Map on the screen

GoogleMap(
            initialCameraPosition: _cameraPosition,
            onMapCreated: (GoogleMapController controller){
              _controller=(controller);
               _controller.animateCamera(

CameraUpdate.newCameraPosition(_cameraPosition));
            },

            markers:_markers ,
            onCameraIdle: (){
              setState(() {

              });
            },
          )

Enter fullscreen mode Exit fullscreen mode

Step 5: Code for Fetch Current Location This Code should be inside async function

Future getCurrentLocation() async {
  LocationPermission permission = await Geolocator.checkPermission();
  if (permission != PermissionStatus.granted) {
    LocationPermission permission = await Geolocator.requestPermission();
    if (permission != PermissionStatus.granted)
      getLocation();
    return;
  }
  getLocation();
}
List<Address> results = [];
getLocation() async
{
  Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  print(position.latitude);



  setState(() {
    latlong=new LatLng(position.latitude, position.longitude);
    _cameraPosition=CameraPosition(target:latlong,zoom: 10.0 );
    if(_controller!=null)
      _controller.animateCamera(

          CameraUpdate.newCameraPosition(_cameraPosition));



    _markers.add(Marker(markerId: MarkerId("a"),draggable:true,position: latlong,icon: 
BitmapDescriptor.defaultMarkerWithHue(

        BitmapDescriptor.hueBlue),onDragEnd: (_currentlatLng){
      latlong = _currentlatLng;

    }));
  });
}
Enter fullscreen mode Exit fullscreen mode

Read Complete Example with code

Discussion (0)

pic
Editor guide