async Task ExecuteLoadSuppliersCommand() { if (IsBusy) { return; } IsBusy = true; try { Geocoder geoCoder = new Geocoder(); Suppliers.Clear(); SupplierPins.Clear(); var suppliers = await SupplierDataStore.GetItemsAsync(true); foreach (var supplier in suppliers) { if (supplier.Latitude == 0 && supplier.Longitude == 0) { var approximateLocations = await geoCoder.GetPositionsForAddressAsync(supplier.Address); foreach (var position in approximateLocations) { supplier.Latitude = position.Latitude; supplier.Longitude = position.Longitude; break; } } var distance = Distance(UserPosition.Latitude, UserPosition.Longitude, supplier.Latitude, supplier.Longitude, 'K'); if (distance <= MapRadius) { Suppliers.Add(supplier); SupplierPins.Add(new CustomPin { Type = PinType.Place, Position = new Position(supplier.Latitude, supplier.Longitude), Label = supplier.Name, Address = supplier.Address, Color = Color.Red, Opacity = 100 }); } } } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsBusy = false; } }
public SuppliersViewModel() { Title = "Suppliers"; Suppliers = new ObservableCollection <Supplier>(); SupplierPins = new ObservableCollection <CustomPin>(); LoadSuppliersCommand = new Command(async() => await ExecuteLoadSuppliersCommand()); MessagingCenter.Subscribe <Homepage, Supplier>(this, "AddSupplier", async(obj, supplier) => { var _supplier = supplier as Supplier; Suppliers.Add(_supplier); await SupplierDataStore.AddItemAsync(_supplier); }); }