import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
class MapScreen extends StatefulWidget {
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
GoogleMapController mapController;
Location location = Location();
LatLng currentLocation;
@override
void initState() {
super.initState();
getCurrentLocation();
}
void getCurrentLocation() 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;
}
}
var userLocation = await location.getLocation();
setState(() {
currentLocation = LatLng(userLocation.latitude, userLocation.longitude);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Google Maps Current Location'),
),
body: GoogleMap(
initialCameraPosition: CameraPosition(
target: currentLocation ?? LatLng(0, 0),
zoom: 14.0,
),
markers: Set<Marker>.from([
Marker(
markerId: MarkerId('currentLocation'),
position: currentLocation ?? LatLng(0, 0),
),
]),
onMapCreated: (GoogleMapController controller) {
mapController = controller;
},
),
);
}
}