Reputation: 21
how to send request to nearest driver in order to booking a ride or cab's, but I want just when I click on request button the driver will automatically found the nearest one. I used geo_fire plugin for nearby drivers but I want to pick nearest driver to the user location and I stored drivers location in list. if any one have any answer or information about this particular problem please share it it will great full to be received thanks.
Future<void> initGeoFire() async {
LocationData position = await _location.getLocation();
locationData = position;
await Geofire.initialize("availableDrivers");
try {
streamSubscriptionQueryAtDriversLoc = Geofire.queryAtLocation(
locationData!.latitude!,
locationData!.longitude!,
nearbyDriversRadius)!
.listen((map) {
// debugPrint(map);
if (map != null) {
var callBack = map['callBack'];
//latitude will be retrieved from map['latitude']
//longitude will be retrieved from map['longitude']
switch (callBack) {
case Geofire.onKeyEntered:
NearbyDrivers nearbyDrivers = NearbyDrivers();
// nearbyDrivers.key ??= map["key"];
nearbyDrivers.key = map["key"];
nearbyDrivers.latitude = map["latitude"];
nearbyDrivers.longitude = map["longitude"];
GeoFireAssistant.neaByDriversList.add(nearbyDrivers);
if (availableDriversStatus == true) {
updateAvailableDriversOnMap();
}
break;
case Geofire.onKeyExited:
// keysRetrieved.remove(map["key"]);
GeoFireAssistant.deleteDriversById(map["key"]);
updateAvailableDriversOnMap();
break;
case Geofire.onKeyMoved:
// Update your key's location
NearbyDrivers nearbyDrivers = NearbyDrivers();
nearbyDrivers.key = map["key"];
nearbyDrivers.latitude = map["latitude"];
nearbyDrivers.longitude = map["longitude"];
GeoFireAssistant.updateDriverOnMoved(nearbyDrivers);
updateAvailableDriversOnMap();
break;
case Geofire.onGeoQueryReady:
updateAvailableDriversOnMap();
// All Initial Data is loaded
debugPrint(map['result']);
break;
}
}
if (!mounted) return;
setState(() {});
});
} catch (e) {
debugPrint(e.toString());
FirebaseErrorHandling().flutterToast("$e");
}
}
class GeoFireAssistant {
static List<NearbyDrivers> neaByDriversList = [];
static void deleteDriversById(String id) {
int delete = neaByDriversList.indexWhere((key) => key.key == id);
class NearbyDrivers {
String? key;
double? latitude;
double? longitude;
NearbyDrivers({this.key, this.latitude, this.longitude});
}
neaByDriversList.removeAt(delete);
}
static void updateDriverOnMoved(NearbyDrivers drivers) {
int index = neaByDriversList.indexWhere((key) => key.key == drivers.key);
neaByDriversList[index].latitude = drivers.latitude;
neaByDriversList[index].longitude = drivers.longitude;
}
Upvotes: 0
Views: 455
Reputation: 9375
You'll need to use geohashing
Geohashing is a geocoding method used to encode geographic coordinates (latitude and longitude) into a short string of digits and letters delineating an area on a map, which is called a cell, with varying resolutions. The more characters in the string, the more precise the location.
In a nutshell, you'll need to calculate a geohash for the location of the customer and calculate geohashes for the locations of the taxis.
By indexing the information this way, that will allow you to find the nearest taxis more quickly.
Hopefully, that's enough to get you started, because I do not know Flutter or Dart, so I can't help you with the implementation details.
Upvotes: 0