-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomePage.java
More file actions
130 lines (120 loc) · 3.5 KB
/
HomePage.java
File metadata and controls
130 lines (120 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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',
)
),
];
final _polyline = <Polyline>[];
// 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),
polylines: Set<Polyline>.of(_polyline),
// 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("1"),
position: LatLng(value.latitude, value.longitude),
infoWindow: InfoWindow(
title: 'My Current Location',
),
)
);
print('${value.latitude + 0.01} ${value.longitude + 0.01}');
_markers.add(
Marker(
markerId: MarkerId("2"),
position: LatLng(value.latitude + 0.01, value.longitude + 0.01
infoWindow: InfoWindow(
title: 'nEW LOCATIONn',
),
)
);
_polyline.add(
Polyline(
polylineId: PolylineId("1"),
visible: true,
points: [
LatLng(value.latitude, value.longitude),
LatLng(value.latitude + 0.01, value.longitude + 0.01),
],
width: 5,
color: Colors.red,
polylineId: PolylineId("distance"),
)
);
// specified current users location
CameraPosition cameraPosition = new CameraPosition(
target: LatLng(value.latitude + 0.01, value.longitude + 0.01),
zoom: 14,
);
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
setState(() {
});
});
},
child: Icon(Icons.local_activity),
),
);
}
}