second commit
This commit is contained in:
132
lib/Controller/Directionmap/Directionmapcontroller.dart
Normal file
132
lib/Controller/Directionmap/Directionmapcontroller.dart
Normal file
@@ -0,0 +1,132 @@
|
||||
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../Helper/Constants/Colorconstants.dart';
|
||||
|
||||
class DirectionMapController extends GetxController{
|
||||
|
||||
String? googleApiKey;
|
||||
|
||||
|
||||
late GoogleMapController mapController;
|
||||
// double _originLatitude = 6.5212402, _originLongitude = 3.3679965;
|
||||
// double _destLatitude = 6.849660, _destLongitude = 3.648190;
|
||||
double? originLatitude;
|
||||
double? originLongitude;
|
||||
double? destLatitude;
|
||||
double? destLongitude;
|
||||
|
||||
static LatLng? center;
|
||||
|
||||
|
||||
Map<MarkerId, Marker> markers = {};
|
||||
Map<PolylineId, Polyline> polylines = {};
|
||||
List<LatLng> polylineCoordinates = [];
|
||||
PolylinePoints polylinePoints = PolylinePoints();
|
||||
|
||||
|
||||
|
||||
|
||||
void onMapCreated(GoogleMapController controller) async {
|
||||
mapController = controller;
|
||||
}
|
||||
|
||||
addMarker(LatLng position, String id, BitmapDescriptor descriptor) {
|
||||
print('originLatitudeaddMarkercontroller $originLatitude');
|
||||
print('originLongitudeaddMarkercontroller $originLongitude');
|
||||
print('destLatitudeaddMarkercontroller $destLatitude');
|
||||
print('destLongitudeaddMarkercontroller $destLongitude');
|
||||
|
||||
MarkerId markerId = MarkerId(id);
|
||||
Marker marker =
|
||||
Marker(markerId: markerId, icon: descriptor, position: position);
|
||||
markers[markerId] = marker;
|
||||
}
|
||||
|
||||
addPolyLine() {
|
||||
PolylineId id = PolylineId("poly");
|
||||
Polyline polyline = Polyline(
|
||||
polylineId: id,
|
||||
color: ColorConstants.primaryColor,
|
||||
points: polylineCoordinates,
|
||||
jointType: JointType.mitered,
|
||||
geodesic: true,
|
||||
visible: true,
|
||||
width: 10
|
||||
|
||||
|
||||
|
||||
|
||||
);
|
||||
polylines[id] = polyline;
|
||||
update();
|
||||
}
|
||||
|
||||
|
||||
void setCameraPosition(List<LatLng> coordinates) {
|
||||
LatLngBounds bounds = boundsFromLatLngList(coordinates);
|
||||
CameraUpdate cameraUpdate = CameraUpdate.newLatLngBounds(bounds, 50); // Adjust padding as needed
|
||||
mapController.animateCamera(cameraUpdate);
|
||||
}
|
||||
|
||||
|
||||
LatLngBounds boundsFromLatLngList(List<LatLng> list) {
|
||||
double? x0, x1, y0, y1;
|
||||
|
||||
for (LatLng latLng in list) {
|
||||
if (x0 == null || latLng.latitude < x0) x0 = latLng.latitude;
|
||||
if (x1 == null || latLng.latitude > x1) x1 = latLng.latitude;
|
||||
if (y0 == null || latLng.longitude < y0) y0 = latLng.longitude;
|
||||
if (y1 == null || latLng.longitude > y1) y1 = latLng.longitude;
|
||||
}
|
||||
|
||||
return LatLngBounds(northeast: LatLng(x1!, y1!), southwest: LatLng(x0!, y0!));
|
||||
}
|
||||
|
||||
|
||||
|
||||
getPolyline() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
print('originLatitudegetPolylinecontroller $originLatitude');
|
||||
print('originLongitudegetPolylinecontroller $originLongitude');
|
||||
print('destLatitudegetPolylinecontroller $destLatitude');
|
||||
print('destLongitudegetPolylinecontroller $destLongitude');
|
||||
googleApiKey = prefs.getString('googleApiKey')??'AIzaSyBhkGfnq27sN0wV5y_S-M2KojpFTk_by-Q';
|
||||
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
|
||||
request: PolylineRequest(
|
||||
origin: PointLatLng(originLatitude ?? 0.0, originLongitude ?? 0.0),
|
||||
destination: PointLatLng(destLatitude ?? 0.0, destLongitude ?? 0.0),
|
||||
mode: TravelMode.driving,
|
||||
// Optional:
|
||||
// wayPoints: [PolylineWayPoint(location: "Some location string")]
|
||||
),
|
||||
googleApiKey: googleApiKey ?? 'YOUR_API_KEY',
|
||||
);
|
||||
|
||||
|
||||
if (result.points.isNotEmpty) {
|
||||
// dropChangeZoom(13);
|
||||
polylineCoordinates.clear();
|
||||
|
||||
result.points.forEach((PointLatLng point) {
|
||||
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
|
||||
|
||||
setCameraPosition(polylineCoordinates);
|
||||
|
||||
});
|
||||
}
|
||||
addPolyLine();
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
// TODO: implement onInit
|
||||
|
||||
|
||||
|
||||
super.onInit();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user