import 'dart:async';
import 'package:flutter/material.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> {
// on below line we are initializing our controller for google maps.
Completer<GoogleMapController> _controller = Completer();
// on below line we are specifying our camera position
static final CameraPosition _kGoogle = const CameraPosition(
target: LatLng(37.42796133580664, -122.885749655962),
zoom: 14.4746,
);
// on below line we have created the list of markers to set on map
List<Marker> _marker = [];
final List<Marker> _list = const [
Marker(
markerId: MarkerId('1'),
position: LatLng(20.42796133580664, 75.885749655962),
infoWindow: InfoWindow(
title: 'My Position',
)
),
Marker(
markerId: MarkerId('2'),
position: LatLng(25.42796133580664, 80.885749655962),
infoWindow: InfoWindow(
title: 'Location 1',
)
),
Marker(
markerId: MarkerId('3'),
position: LatLng(23.42796133580664, 77.885749655962),
infoWindow: InfoWindow(
title: 'Location 2',
)
),
Marker(
markerId: MarkerId('4'),
position: LatLng(22.42796133580664, 78.885749655962),
infoWindow: InfoWindow(
title: 'Location 3',
)
),
Marker(
markerId: MarkerId('5'),
position: LatLng(21.42796133580664, 79.885749655962),
infoWindow: InfoWindow(
title: 'Location 4',
)
),
Marker(
markerId: MarkerId('6'),
position: LatLng(20.42796133580664, 73.885749655962),
infoWindow: InfoWindow(
title: 'Location 5',
)
),
];
@override
void initState() {
// TODO: implement initState
super.initState();
_marker.addAll(_list);
}
@override
Widget build(BuildContext context) {
return Scaffold(
// on below line we are specifying our app bar.
appBar: AppBar(
// setting background color for app bar
backgroundColor: Color(0xFF0F9D58),
// setting title for app bar.
title: Text("Google Maps"),
),
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 below line we are creating floating action
// button for changing camera position
floatingActionButton: FloatingActionButton(
onPressed: () async{
GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(
// on below line we have given positions of Location 5
CameraPosition(
target: LatLng(20.42796133580664, 73.885749655962),
zoom: 14,
)
));
setState(() {
});
},
// on below line we have set icon for button
child: Icon(Icons.location_disabled_outlined),
),
);
}
}