public void FindDrivers() { findDriverRef.ObserveSingleEvent(DataEventType.Value, (DataSnapshot snapshot) => { if (snapshot.GetValue <NSObject>() != null) { //Converts Datasnapshot to a dictionary to retrieve the keys; var snapShotData = snapshot.GetValue <NSDictionary>(); foreach (NSString key in snapShotData.Keys) { if (snapshot.GetChildSnapshot(key).GetChildSnapshot("ride_id").GetValue <NSObject>() != NSNull.Null) { string ride_id = snapshot.GetChildSnapshot(key).GetChildSnapshot("ride_id").GetValue <NSObject>().ToString(); if (ride_id == "waiting") { // Fetch Location Coordinates string latitudeString = snapshot.GetChildSnapshot(key).GetChildSnapshot("location").GetChildSnapshot("latitude").GetValue <NSObject>().ToString(); string longitudeString = snapshot.GetChildSnapshot(key).GetChildSnapshot("location").GetChildSnapshot("longitude").GetValue <NSObject>().ToString(); CLLocationCoordinate2D driverLocation = new CLLocationCoordinate2D(double.Parse(latitudeString), double.Parse(longitudeString)); // Compute Distance Between Pickup Location and Driver Location (KM) double distanceFromPickup = (Google.Maps.GeometryUtils.Distance(pickupLocation, driverLocation)) / 1000; if (distanceFromPickup <= 50) { // available driver AvailableDriver driver = new AvailableDriver(); driver.ID = key; driver.DistanceFromPickup = distanceFromPickup; availableDrivers.Add(driver); } } } } if (availableDrivers.Count > 0) { // Sort drivers to closest distance availableDrivers = availableDrivers.OrderBy(o => o.DistanceFromPickup).ToList(); DriversFound?.Invoke(this, new DriversFoundEventArgs { Drivers = availableDrivers }); } else { DriverNotFound.Invoke(this, new EventArgs()); } } }); }
public void OnDataChange(DataSnapshot snapshot) { if (snapshot.Value != null) { var child = snapshot.Children.ToEnumerable <DataSnapshot>(); availableDrivers.Clear(); foreach (DataSnapshot data in child) { if (data.Child("ride_id").Value != null) { if (data.Child("ride_id").Value.ToString() == "waiting") { //Get Driver Location double latitude = double.Parse(data.Child("location").Child("latitude").Value.ToString()); double longitude = double.Parse(data.Child("location").Child("longitude").Value.ToString()); LatLng driverLocation = new LatLng(latitude, longitude); AvailableDriver driver = new AvailableDriver(); //compute Distance between Pickup Location and Driver Location driver.DistanceFromPickup = SphericalUtil.ComputeDistanceBetween(mPickupLoction, driverLocation); driver.ID = data.Key; availableDrivers.Add(driver); } } } if (availableDrivers.Count > 0) { availableDrivers = availableDrivers.OrderBy(o => o.DistanceFromPickup).ToList(); DriversFound?.Invoke(this, new DriverFoundEventArgs { Drivers = availableDrivers }); } else { DriverNotFound.Invoke(this, new EventArgs()); } } else { DriverNotFound.Invoke(this, new EventArgs()); } }
public void Create(List <ActiveDrivers> activeDrivers) { availableDrivers = activeDrivers; if (availableDrivers.Count > 0) { availableDrivers.ForEach(x => { LatLng driverLocation = new LatLng(Convert.ToDouble(x.Latitude), Convert.ToDouble(x.Longitude)); LatLng pickUpLocation = new LatLng(Convert.ToDouble(rides.RidesInfo.LocationInfo.PickupLatitude), Convert.ToDouble(rides.RidesInfo.LocationInfo.PickupLongitude)); x.DistanceFromPickup = SphericalUtil.ComputeDistanceBetween(pickUpLocation, driverLocation); }); availableDrivers = availableDrivers.OrderBy(o => o.DistanceFromPickup).ToList(); DriversFound?.Invoke(this, new DriverFoundEventArgs { Drivers = availableDrivers, Ride = rides }); } else { DriverNotFound.Invoke(this, new EventArgs()); } }