You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.6 KiB
121 lines
3.6 KiB
import 'dart:developer';
|
|
import 'dart:ui' as ui;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
|
import 'package:location/location.dart';
|
|
|
|
import 'app_images.dart';
|
|
import 'app_sizes.dart';
|
|
import 'permission_alert.dart';
|
|
|
|
class LocationController<T> extends GetxController {
|
|
Location location = Location();
|
|
|
|
// final Rx<LatLng?> locationPosition = const LatLng(0.0, 0.0).obs;
|
|
/*final Rx<LatLng?> locationPosition =
|
|
const LatLng(12.90618717, 77.5844983).obs;*/
|
|
final Rx<LatLng?> locationPosition =
|
|
const LatLng(0, 0).obs;
|
|
|
|
bool locationServiceActive = true;
|
|
|
|
BitmapDescriptor? pickupMarker;
|
|
BitmapDescriptor? dropMarker;
|
|
|
|
@override
|
|
void onInit() async {
|
|
|
|
await _getBytesFromAsset(AppImages.pickupMarker, AppSizes.mapPinSize * 0.1);
|
|
await _getBytesFromAsset(AppImages.dropMarker, AppSizes.mapPinSize * 0.05);
|
|
|
|
super.onInit();
|
|
refreshToLiveLocation();
|
|
}
|
|
|
|
Future<void> _getBytesFromAsset(String path, double size) async {
|
|
ByteData data = await rootBundle.load(path);
|
|
ui.Codec codec = await ui.instantiateImageCodec(
|
|
data.buffer.asUint8List(),
|
|
targetWidth: size.toInt(),
|
|
allowUpscaling: true,
|
|
);
|
|
ui.FrameInfo fi = await codec.getNextFrame();
|
|
if (path == AppImages.pickupMarker) {
|
|
pickupMarker = BitmapDescriptor.fromBytes(
|
|
(await fi.image.toByteData(format: ui.ImageByteFormat.png))!
|
|
.buffer
|
|
.asUint8List());
|
|
} else if (path == AppImages.dropMarker) {
|
|
dropMarker = BitmapDescriptor.fromBytes(
|
|
(await fi.image.toByteData(format: ui.ImageByteFormat.png))!
|
|
.buffer
|
|
.asUint8List());
|
|
} else {}
|
|
}
|
|
|
|
refreshToLiveLocation() async {
|
|
log("initiating");
|
|
bool serviceEnabled;
|
|
|
|
PermissionStatus permissionGranted;
|
|
|
|
serviceEnabled = await location.serviceEnabled();
|
|
|
|
if (!serviceEnabled) {
|
|
serviceEnabled = await location.requestService();
|
|
locationPosition.value = null;
|
|
return;
|
|
}
|
|
log("permission check");
|
|
permissionGranted = await location.hasPermission();
|
|
|
|
if (permissionGranted == PermissionStatus.denied) {
|
|
permissionGranted = await location.requestPermission();
|
|
if (permissionGranted != PermissionStatus.granted) {
|
|
showPermissionAlertDialog(
|
|
requestMsg:
|
|
"Location access needed. Go to Android settings, tap App permissions and tap Allow.",
|
|
barrierDismissible: false,
|
|
);
|
|
} else {
|
|
await location.changeSettings(
|
|
accuracy: LocationAccuracy.balanced,
|
|
interval: 5000,
|
|
distanceFilter: 2);
|
|
|
|
location.onLocationChanged.listen((LocationData currentLocation) async {
|
|
var lat = currentLocation.latitude;
|
|
var long = currentLocation.longitude;
|
|
if (lat != null && long != null) {
|
|
locationPosition.value = LatLng(
|
|
lat,
|
|
long,
|
|
);
|
|
log("live location ${locationPosition.value}");
|
|
}
|
|
});
|
|
}
|
|
} else {
|
|
await location.changeSettings(
|
|
accuracy: LocationAccuracy.balanced,
|
|
interval: 5000,
|
|
distanceFilter: 2);
|
|
|
|
location.onLocationChanged.listen((LocationData currentLocation) async {
|
|
var lat = currentLocation.latitude;
|
|
var long = currentLocation.longitude;
|
|
if (lat != null && long != null) {
|
|
locationPosition.value = LatLng(
|
|
lat,
|
|
long,
|
|
);
|
|
log("live location ${locationPosition.value}");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|