-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy path02. Google_maps_polylines.dart
121 lines (104 loc) · 4.16 KB
/
02. Google_maps_polylines.dart
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
import 'dart:async';
import 'dart:io';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';
import 'package:provider/provider.dart';
// NOTES :
// 1. DM ME FOR FULL PROJECT CODE
// 2. YOU NEED TO HAVE BILLING ACCOUNT ON GOOGLE PLATFORM
// 3. STAR THIS REPOSITORY
// 4. FOLLOW ME ON INSTAGRAM : [https://www.instagram.com/abhishvek/]
class ShowRoutesView extends StatefulWidget {
@override
State<ShowRoutesView> createState() => _ShowRoutesViewState();
}
class _ShowRoutesViewState extends State<ShowRoutesView> {
List<LatLng> polylineCoordinates = [];
late PolylinePoints polylinePoints;
final Set<Polyline> _polylines = <Polyline>{};
GoogleMapController? googleMapController;
Location? location;
@override
void initState() {
polylinePoints = PolylinePoints();
location = Location();
super.initState();
}
@override
Widget build(BuildContext context) {
double ZOOM = 12;
LatLng? currentLatLong =
Provider.of<LocationNotifier>(context, listen: true).currentLatLng;
String API_KEY = "API_KEY";
void setPolylines() async {
LatLng desLatLng = "SOME_LATLNG";
LatLng myLocationLatLng = "SOME_LATLNG";
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
API_KEY,
PointLatLng(desLatLng.latitude, desLatLng.longitude),
PointLatLng(myLocationLatLng.latitude, myLocationLatLng.longitude));
if (result.status == 'OK') {
for (var point in result.points) {
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
}
setState(() {
_polylines.add(Polyline(
polylineId: const PolylineId('id'),
points: polylineCoordinates));
});
}
}
LatLng desLatLng = "SOME_LATLNG";
Marker desMarker = Marker(
infoWindow: const InfoWindow(title: "Destination point"),
markerId: MarkerId(YOUR_OBJECT.toString()),
position: desLatLng);
Marker myLocationMarker = Marker(
infoWindow: const InfoWindow(title: "My location"),
markerId: MarkerId(currentLatLong.toString()),
position: currentLatLong!);
Set<Marker> marker = {desMarker, myLocationMarker};
Completer<GoogleMapController> _controller = Completer();
return Scaffold(
body: Expanded(child: Consumer2<LocationNotifier, YourCustomNotifier>(
builder: ((context, locationNotifier, kNotifier, child) {
return GoogleMap(
scrollGesturesEnabled: true,
buildingsEnabled: true,
zoomControlsEnabled: true,
zoomGesturesEnabled: true,
onTap: (latLng) {
_polylines.clear();
polylineCoordinates.clear();
setPolylines();
locationNotifier.animateToCamera(
isCurrent: false,
zoom: 12,
otherLatLng: latLng,
controller: googleMapController!);
},
polylines: _polylines,
myLocationButtonEnabled: true,
myLocationEnabled: true,
markers: marker,
mapType: MapType.normal,
initialCameraPosition: CameraPosition(
target: currentLatLong,
zoom: ZOOM),
onMapCreated: (GoogleMapController mainController) {
_controller.complete(mainController);
googleMapController = mainController;
setPolylines();
},
);
}),
))
}
}