xxxxxxxxxx
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;
},
),
);
}
}
xxxxxxxxxx
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class Map extends StatefulWidget {
@override
_MapState createState() => _MapState();
}
class _MapState extends State<Map> {
Completer<GoogleMapController> controller1;
//static LatLng _center = LatLng(-15.4630239974464, 28.363397732282127);
static LatLng _initialPosition;
final Set<Marker> _markers = {};
static LatLng _lastMapPosition = _initialPosition;
@override
void initState() {
super.initState();
_getUserLocation();
}
void _getUserLocation() async {
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
List<Placemark> placemark = await Geolocator().placemarkFromCoordinates(position.latitude, position.longitude);
setState(() {
_initialPosition = LatLng(position.latitude, position.longitude);
print('${placemark[0].name}');
});
}
_onMapCreated(GoogleMapController controller) {
setState(() {
controller1.complete(controller);
});
}
MapType _currentMapType = MapType.normal;
void _onMapTypeButtonPressed() {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.satellite
: MapType.normal;
});
}
_onCameraMove(CameraPosition position) {
_lastMapPosition = position.target;
}
_onAddMarkerButtonPressed() {
setState(() {
_markers.add(
Marker(
markerId: MarkerId(_lastMapPosition.toString()),
position: _lastMapPosition,
infoWindow: InfoWindow(
title: "Pizza Parlour",
snippet: "This is a snippet",
onTap: (){
}
),
onTap: (){
},
icon: BitmapDescriptor.defaultMarker));
});
}
Widget mapButton(Function function, Icon icon, Color color) {
return RawMaterialButton(
onPressed: function,
child: icon,
shape: new CircleBorder(),
elevation: 2.0,
fillColor: color,
padding: const EdgeInsets.all(7.0),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _initialPosition == null ? Container(child: Center(child:Text('loading map..', style: TextStyle(fontFamily: 'Avenir-Medium', color: Colors.grey[400]),),),) : Container(
child: Stack(children: <Widget>[
GoogleMap(
markers: _markers,
mapType: _currentMapType,
initialCameraPosition: CameraPosition(
target: _initialPosition,
zoom: 14.4746,
),
onMapCreated: _onMapCreated,
zoomGesturesEnabled: true,
onCameraMove: _onCameraMove,
myLocationEnabled: true,
compassEnabled: true,
myLocationButtonEnabled: false,
),
Align(
alignment: Alignment.topRight,
child: Container(
margin: EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0),
child: Column(
children: <Widget>[
mapButton(_onAddMarkerButtonPressed,
Icon(
Icons.add_location
), Colors.blue),
mapButton(
_onMapTypeButtonPressed,
Icon(
IconData(0xf473,
fontFamily: CupertinoIcons.iconFont,
fontPackage: CupertinoIcons.iconFontPackage),
),
Colors.green),
],
)),
)
]),
),
);
}
}
xxxxxxxxxx
Future<void> _gotoUserCurrentLocation() async {
final _curentPosition = _userLocationSnapshotCache.position;
final GoogleMapController _controller = await _mapController.future;
_controller.animateCamera(
CameraUpdate.newCameraPosition(
MapUtil.initialCameraPosition(position: _curentPosition),
),
);
}
xxxxxxxxxx
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Completer<GoogleMapController> _controller = Completer();
// on below line we have specified camera position
static final CameraPosition _kGoogle = const CameraPosition(
target: LatLng(20.42796133580664, 80.885749655962),
zoom: 14.4746,
);
// on below line we have created the list of markers
final List<Marker> _markers = <Marker>[
Marker(
markerId: MarkerId('1'),
position: LatLng(20.42796133580664, 75.885749655962),
infoWindow: InfoWindow(
title: 'My Position',
)
),
];
// created method for getting user current location
Future<Position> getUserCurrentLocation() async {
await Geolocator.requestPermission().then((value){
}).onError((error, stackTrace) async {
await Geolocator.requestPermission();
print("ERROR"+error.toString());
});
return await Geolocator.getCurrentPosition();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF0F9D58),
// on below line we have given title of app
title: Text("GFG"),
),
body: Container(
child: SafeArea(
// on below line creating google maps
child: GoogleMap(
// on below line setting camera position
initialCameraPosition: _kGoogle,
// on below line we are setting markers on the map
markers: Set<Marker>.of(_marker),
// on below line specifying map type.
mapType: MapType.normal,
// on below line setting user location enabled.
myLocationEnabled: true,
// on below line setting compass enabled.
compassEnabled: true,
// on below line specifying controller on map complete.
onMapCreated: (GoogleMapController controller){
_controller.complete(controller);
},
),
),
),
// on pressing floating action button the camera will take to user current location
floatingActionButton: FloatingActionButton(
onPressed: () async{
getUserCurrentLocation().then((value) async {
print(value.latitude.toString() +" "+value.longitude.toString());
// marker added for current users location
_markers.add(
Marker(
markerId: MarkerId("2"),
position: LatLng(value.latitude, value.longitude),
infoWindow: InfoWindow(
title: 'My Current Location',
),
)
);
// specified current users location
CameraPosition cameraPosition = new CameraPosition(
target: LatLng(value.latitude, value.longitude),
zoom: 14,
);
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
setState(() {
});
});
},
child: Icon(Icons.local_activity),
),
);
}
}