flutter user current location code example
Example: currunt location in flutter
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
void main() => runApp(MapScreen());
class MapScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Map",
home: MapActivity(),
);
}
}
class MapActivity extends StatefulWidget {
@override
_MapActivityState createState() => _MapActivityState();
}
class _MapActivityState extends State {
LatLng _center ;
Position currentLocation;
@override
void initState() {
// TODO: implement initState
super.initState();
getUserLocation();
}
Future locateUser() async {
return Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
getUserLocation() async {
currentLocation = await locateUser();
setState(() {
_center = LatLng(currentLocation.latitude, currentLocation.longitude);
});
print('center $_center');
}
}