Skip to content

Commit f2b86c1

Browse files
committed
pick destination from map
1 parent cb98598 commit f2b86c1

File tree

5 files changed

+142
-62
lines changed

5 files changed

+142
-62
lines changed

images/pick.png

4.01 KB
Loading

lib/main.dart

Lines changed: 128 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter_uber_location/navigation_screen.dart';
5+
import 'package:geocoder2/geocoder2.dart';
6+
import 'package:google_maps_flutter/google_maps_flutter.dart';
7+
import 'package:location/location.dart';
8+
import 'package:location/location.dart' as loc;
39

410
main() {
511
runApp(MaterialApp(
@@ -13,57 +19,140 @@ class MyApp extends StatefulWidget {
1319
}
1420

1521
class _MyAppState extends State<MyApp> {
16-
TextEditingController latController = TextEditingController();
17-
TextEditingController lngController = TextEditingController();
22+
LatLng? destLocation = LatLng(40.7128, -74.0060);
23+
Location location = Location();
24+
loc.LocationData? _currentPosition;
25+
final Completer<GoogleMapController?> _controller = Completer();
26+
String? _address;
27+
28+
@override
29+
void initState() {
30+
// TODO: implement initState
31+
super.initState();
32+
getCurrentLocation();
33+
}
34+
1835
@override
1936
Widget build(BuildContext context) {
2037
return Scaffold(
2138
appBar: AppBar(
2239
title: Text('Flutter uber'),
2340
),
24-
body: Padding(
25-
padding: const EdgeInsets.all(18.0),
26-
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
27-
Text(
28-
'Enter your location',
29-
style: TextStyle(fontSize: 40),
30-
),
31-
SizedBox(
32-
height: 30,
33-
),
34-
TextField(
35-
controller: latController,
36-
decoration: InputDecoration(
37-
border: OutlineInputBorder(),
38-
labelText: 'latitude',
41+
floatingActionButton: FloatingActionButton(
42+
child: Icon(Icons.navigate_next),
43+
onPressed: () {
44+
Navigator.of(context).pushAndRemoveUntil(
45+
MaterialPageRoute(
46+
builder: (context) => NavigationScreen(
47+
destLocation!.latitude, destLocation!.longitude),
48+
),
49+
(route) => false);
50+
}),
51+
body: Stack(
52+
children: [
53+
GoogleMap(
54+
55+
zoomControlsEnabled: false,
56+
initialCameraPosition: CameraPosition(
57+
target: destLocation!,
58+
zoom: 16,
3959
),
60+
onCameraMove: (CameraPosition? position) {
61+
if (destLocation != position!.target) {
62+
setState(() {
63+
destLocation = position.target;
64+
});
65+
}
66+
},
67+
onCameraIdle: () {
68+
print('camera idle');
69+
getAddressFromLatLng();
70+
},
71+
onTap: (latLng) {
72+
print(latLng);
73+
},
74+
onMapCreated: (GoogleMapController controller) {
75+
_controller.complete(controller);
76+
},
4077
),
41-
SizedBox(
42-
height: 20,
43-
),
44-
TextField(
45-
controller: lngController,
46-
decoration: InputDecoration(
47-
border: OutlineInputBorder(),
48-
labelText: 'longitute',
78+
Align(
79+
alignment: Alignment.center,
80+
child: Padding(
81+
padding: const EdgeInsets.only(bottom: 35.0),
82+
child: Image.asset(
83+
'images/pick.png',
84+
height: 45,
85+
width: 45,
86+
),
4987
),
5088
),
51-
SizedBox(
52-
height: 20,
53-
),
54-
Container(
55-
width: double.infinity,
56-
child: ElevatedButton(
57-
onPressed: () {
58-
Navigator.of(context).push(MaterialPageRoute(
59-
builder: (context) => NavigationScreen(
60-
double.parse(latController.text),
61-
double.parse(lngController.text))));
62-
},
63-
child: Text('Get Directions')),
89+
Positioned(
90+
top: 40,
91+
right: 20,
92+
left: 20,
93+
child: Container(
94+
decoration: BoxDecoration(
95+
border: Border.all(color: Colors.black),
96+
color: Colors.white,
97+
),
98+
padding: EdgeInsets.all(20),
99+
child: Text(_address ?? 'Pick your destination address',
100+
overflow: TextOverflow.visible, softWrap: true),
101+
),
64102
),
65-
]),
103+
],
66104
),
67105
);
68106
}
107+
108+
getAddressFromLatLng() async {
109+
try {
110+
GeoData data = await Geocoder2.getDataFromCoordinates(
111+
latitude: destLocation!.latitude,
112+
longitude: destLocation!.longitude,
113+
googleMapApiKey: "YOUR_API_KEY");
114+
setState(() {
115+
_address = data.address;
116+
});
117+
} catch (e) {
118+
print(e);
119+
}
120+
}
121+
122+
getCurrentLocation() async {
123+
bool _serviceEnabled;
124+
PermissionStatus _permissionGranted;
125+
126+
_serviceEnabled = await location.serviceEnabled();
127+
final GoogleMapController? controller = await _controller.future;
128+
129+
if (!_serviceEnabled) {
130+
_serviceEnabled = await location.requestService();
131+
if (!_serviceEnabled) {
132+
return;
133+
}
134+
}
135+
136+
_permissionGranted = await location.hasPermission();
137+
if (_permissionGranted == PermissionStatus.denied) {
138+
_permissionGranted = await location.requestPermission();
139+
if (_permissionGranted != PermissionStatus.granted) {
140+
return;
141+
}
142+
}
143+
if (_permissionGranted == loc.PermissionStatus.granted) {
144+
location.changeSettings(accuracy: loc.LocationAccuracy.high);
145+
146+
_currentPosition = await location.getLocation();
147+
controller?.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
148+
target:
149+
LatLng(_currentPosition!.latitude!, _currentPosition!.longitude!),
150+
zoom: 16,
151+
)));
152+
setState(() {
153+
destLocation =
154+
LatLng(_currentPosition!.latitude!, _currentPosition!.longitude!);
155+
});
156+
}
157+
}
69158
}

lib/navigation_screen.dart

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class _NavigationScreenState extends State<NavigationScreen> {
2626
Location location = Location();
2727
Marker? sourcePosition, destinationPosition;
2828
loc.LocationData? _currentPosition;
29-
LatLng curLocation = LatLng(23.0525, 72.5667);
29+
LatLng curLocation = LatLng(40.7128, -74.0060);
3030
StreamSubscription<loc.LocationData>? locationSubscription;
3131

3232
@override
@@ -104,27 +104,10 @@ class _NavigationScreenState extends State<NavigationScreen> {
104104
}
105105

106106
getNavigation() async {
107-
bool _serviceEnabled;
108-
PermissionStatus _permissionGranted;
107+
109108
final GoogleMapController? controller = await _controller.future;
110109
location.changeSettings(accuracy: loc.LocationAccuracy.high);
111-
_serviceEnabled = await location.serviceEnabled();
112-
113-
if (!_serviceEnabled) {
114-
_serviceEnabled = await location.requestService();
115-
if (!_serviceEnabled) {
116-
return;
117-
}
118-
}
119-
120-
_permissionGranted = await location.hasPermission();
121-
if (_permissionGranted == PermissionStatus.denied) {
122-
_permissionGranted = await location.requestPermission();
123-
if (_permissionGranted != PermissionStatus.granted) {
124-
return;
125-
}
126-
}
127-
if (_permissionGranted == loc.PermissionStatus.granted) {
110+
128111
_currentPosition = await location.getLocation();
129112
curLocation =
130113
LatLng(_currentPosition!.latitude!, _currentPosition!.longitude!);
@@ -159,7 +142,7 @@ class _NavigationScreenState extends State<NavigationScreen> {
159142
getDirections(LatLng(widget.lat, widget.lng));
160143
}
161144
});
162-
}
145+
163146
}
164147

165148
getDirections(LatLng dst) async {

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ packages:
9393
description: flutter
9494
source: sdk
9595
version: "0.0.0"
96+
geocoder2:
97+
dependency: "direct main"
98+
description:
99+
name: geocoder2
100+
url: "https://pub.dartlang.org"
101+
source: hosted
102+
version: "1.1.2"
96103
google_maps_flutter:
97104
dependency: "direct main"
98105
description:

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies:
3838
flutter_polyline_points: ^1.0.0
3939
google_maps_flutter: ^2.1.9
4040
url_launcher: ^6.1.5
41+
geocoder2: ^1.1.2
4142

4243
dev_dependencies:
4344
flutter_test:
@@ -62,8 +63,8 @@ flutter:
6263
uses-material-design: true
6364

6465
# To add assets to your application, add an assets section, like this:
65-
# assets:
66-
# - images/a_dot_burr.jpeg
66+
assets:
67+
- images/
6768
# - images/a_dot_ham.jpeg
6869

6970
# An image asset can refer to one or more resolution-specific "variants", see

0 commit comments

Comments
 (0)