diff --git a/README.md b/README.md index 93c17b5..e8c0509 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,34 @@ -# flutter_uber_location +# Flutter Map Uber Style Par 2 -A new Flutter project. +Source Code for Flutter Map Uber Style.
-## Getting Started +## App Features +- Pick Destination from map.
+- Show markers for source and destination locations. +- Draw polyline for the closest path between source and destination.
+- Navigate destination to google map app and use route direction.
+

-This project is a starting point for a Flutter application. -A few resources to get you started if this is your first Flutter project: -- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) -- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) -For help getting started with Flutter, view our -[online documentation](https://flutter.dev/docs), which offers tutorials, -samples, guidance on mobile development, and a full API reference. +https://user-images.githubusercontent.com/41961773/181871029-ecceff0a-1539-40c2-a895-c76e901af47e.mp4 + + + + +For Step By Step Guide Follow Video Tutorials: +- [Flutter Map Uber Style Part 2 | Pick Destination From Map](https://bit.ly/3cIld0t) + +## Links used in project: + +- [Cloud Console](https://bit.ly/3qLUjY9) +- [Location Package](https://bit.ly/2XcsBcX) +- [Google Map Package](https://bit.ly/2Vu1SYC) +- [Flutter Polyline Package](https://bit.ly/3PALuwq) +- [Flutter Url Launcher Package](https://bit.ly/3IYXlSp) +- [Geocoder2 Package](https://bit.ly/3S60MLd) +

+ +Feel Free to copy the code and use it.

+Don't forget to star the repo and like the video :) diff --git a/images/pick.png b/images/pick.png new file mode 100644 index 0000000..9b5a6d6 Binary files /dev/null and b/images/pick.png differ diff --git a/lib/main.dart b/lib/main.dart index bd58fe6..419cd0c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,11 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:flutter_uber_location/navigation_screen.dart'; +import 'package:geocoder2/geocoder2.dart'; +import 'package:google_maps_flutter/google_maps_flutter.dart'; +import 'package:location/location.dart'; +import 'package:location/location.dart' as loc; main() { runApp(MaterialApp( @@ -13,57 +19,140 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - TextEditingController latController = TextEditingController(); - TextEditingController lngController = TextEditingController(); + LatLng? destLocation = LatLng(40.7128, -74.0060); + Location location = Location(); + loc.LocationData? _currentPosition; + final Completer _controller = Completer(); + String? _address; + + @override + void initState() { + // TODO: implement initState + super.initState(); + getCurrentLocation(); + } + @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Flutter uber'), ), - body: Padding( - padding: const EdgeInsets.all(18.0), - child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [ - Text( - 'Enter your location', - style: TextStyle(fontSize: 40), - ), - SizedBox( - height: 30, - ), - TextField( - controller: latController, - decoration: InputDecoration( - border: OutlineInputBorder(), - labelText: 'latitude', + floatingActionButton: FloatingActionButton( + child: Icon(Icons.navigate_next), + onPressed: () { + Navigator.of(context).pushAndRemoveUntil( + MaterialPageRoute( + builder: (context) => NavigationScreen( + destLocation!.latitude, destLocation!.longitude), + ), + (route) => false); + }), + body: Stack( + children: [ + GoogleMap( + + zoomControlsEnabled: false, + initialCameraPosition: CameraPosition( + target: destLocation!, + zoom: 16, ), + onCameraMove: (CameraPosition? position) { + if (destLocation != position!.target) { + setState(() { + destLocation = position.target; + }); + } + }, + onCameraIdle: () { + print('camera idle'); + getAddressFromLatLng(); + }, + onTap: (latLng) { + print(latLng); + }, + onMapCreated: (GoogleMapController controller) { + _controller.complete(controller); + }, ), - SizedBox( - height: 20, - ), - TextField( - controller: lngController, - decoration: InputDecoration( - border: OutlineInputBorder(), - labelText: 'longitute', + Align( + alignment: Alignment.center, + child: Padding( + padding: const EdgeInsets.only(bottom: 35.0), + child: Image.asset( + 'images/pick.png', + height: 45, + width: 45, + ), ), ), - SizedBox( - height: 20, - ), - Container( - width: double.infinity, - child: ElevatedButton( - onPressed: () { - Navigator.of(context).push(MaterialPageRoute( - builder: (context) => NavigationScreen( - double.parse(latController.text), - double.parse(lngController.text)))); - }, - child: Text('Get Directions')), + Positioned( + top: 40, + right: 20, + left: 20, + child: Container( + decoration: BoxDecoration( + border: Border.all(color: Colors.black), + color: Colors.white, + ), + padding: EdgeInsets.all(20), + child: Text(_address ?? 'Pick your destination address', + overflow: TextOverflow.visible, softWrap: true), + ), ), - ]), + ], ), ); } + + getAddressFromLatLng() async { + try { + GeoData data = await Geocoder2.getDataFromCoordinates( + latitude: destLocation!.latitude, + longitude: destLocation!.longitude, + googleMapApiKey: "YOUR_API_KEY"); + setState(() { + _address = data.address; + }); + } catch (e) { + print(e); + } + } + + getCurrentLocation() async { + bool _serviceEnabled; + PermissionStatus _permissionGranted; + + _serviceEnabled = await location.serviceEnabled(); + final GoogleMapController? controller = await _controller.future; + + 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; + } + } + if (_permissionGranted == loc.PermissionStatus.granted) { + location.changeSettings(accuracy: loc.LocationAccuracy.high); + + _currentPosition = await location.getLocation(); + controller?.animateCamera(CameraUpdate.newCameraPosition(CameraPosition( + target: + LatLng(_currentPosition!.latitude!, _currentPosition!.longitude!), + zoom: 16, + ))); + setState(() { + destLocation = + LatLng(_currentPosition!.latitude!, _currentPosition!.longitude!); + }); + } + } } diff --git a/lib/navigation_screen.dart b/lib/navigation_screen.dart index 592bdff..eef0b0d 100644 --- a/lib/navigation_screen.dart +++ b/lib/navigation_screen.dart @@ -26,7 +26,7 @@ class _NavigationScreenState extends State { Location location = Location(); Marker? sourcePosition, destinationPosition; loc.LocationData? _currentPosition; - LatLng curLocation = LatLng(23.0525, 72.5667); + LatLng curLocation = LatLng(40.7128, -74.0060); StreamSubscription? locationSubscription; @override @@ -104,27 +104,10 @@ class _NavigationScreenState extends State { } getNavigation() async { - bool _serviceEnabled; - PermissionStatus _permissionGranted; + final GoogleMapController? controller = await _controller.future; location.changeSettings(accuracy: loc.LocationAccuracy.high); - _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; - } - } - if (_permissionGranted == loc.PermissionStatus.granted) { + _currentPosition = await location.getLocation(); curLocation = LatLng(_currentPosition!.latitude!, _currentPosition!.longitude!); @@ -159,7 +142,7 @@ class _NavigationScreenState extends State { getDirections(LatLng(widget.lat, widget.lng)); } }); - } + } getDirections(LatLng dst) async { diff --git a/pubspec.lock b/pubspec.lock index ea03cbe..be222e8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -93,6 +93,13 @@ packages: description: flutter source: sdk version: "0.0.0" + geocoder2: + dependency: "direct main" + description: + name: geocoder2 + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.2" google_maps_flutter: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 7d65593..4b00574 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: flutter_polyline_points: ^1.0.0 google_maps_flutter: ^2.1.9 url_launcher: ^6.1.5 + geocoder2: ^1.1.2 dev_dependencies: flutter_test: @@ -62,8 +63,8 @@ flutter: uses-material-design: true # To add assets to your application, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg + assets: + - images/ # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see