location adjustment

This commit is contained in:
2022-04-11 15:12:34 +08:00
parent 1072a1a15c
commit 776cc7da49
29 changed files with 119 additions and 14 deletions

View File

@ -10,6 +10,7 @@
BindingContext="{x:Reference billPage}">
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary" IconImageSource="pin.png" Command="{Binding ViewLocation}"/>
<ToolbarItem Order="Primary" IconImageSource="check.png" Command="{Binding CheckBill}"/>
</ContentPage.ToolbarItems>
@ -49,9 +50,6 @@
<ui:OptionEntryCell Height="44" Icon="online.png"
Title="{r:Text Store}"
Text="{Binding Store, Mode=TwoWay}"/>
<ui:OptionSelectCell Height="44"
Detail="{r:Text ViewLocation}"
Command="{Binding ViewLocation}"/>
<ui:OptionDatePickerCell Height="44" Icon="bars.png"
Title="{r:Text CreatedTime}"
Date="{Binding CreatedDate, Mode=TwoWay}"/>

View File

@ -316,6 +316,7 @@ namespace Billing.Views
Longitude = location.Longitude,
Latitude = location.Latitude
});
page.Synced += (sender, loc) => location = loc;
await Navigation.PushAsync(page);
}
}

View File

@ -1,17 +1,37 @@
using Billing.Models;
using System;
using System.Threading;
using System.Threading.Tasks;
using Billing.Models;
using Billing.UI;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
using Map = Xamarin.Forms.Maps.Map;
namespace Billing.Views
{
public class ViewLocationPage : BillingPage
{
public event EventHandler<Location> Synced;
private readonly Bill bill;
private CancellationTokenSource tokenSource;
public ViewLocationPage(Bill bill)
{
On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(false);
this.bill = bill;
Title = bill.Name;
ToolbarItems.Add(new ToolbarItem
{
IconImageSource = "location.png",
Order = ToolbarItemOrder.Primary,
Command = new Command(OnSynced)
});
if (bill.Latitude != null && bill.Longitude != null)
{
var (longitude, latitude) = (bill.Longitude.Value, bill.Latitude.Value).Wgs84ToGcj02();
@ -32,5 +52,78 @@ namespace Billing.Views
};
}
}
protected override void OnDisappearing()
{
if (tokenSource != null && !tokenSource.IsCancellationRequested)
{
tokenSource.Cancel();
}
base.OnDisappearing();
}
private async void OnSynced()
{
if (Tap.IsBusy)
{
return;
}
using (Tap.Start())
{
if (tokenSource != null)
{
return;
}
var location = await GetCurrentLocation();
if (location != null)
{
Synced?.Invoke(this, location);
MainThread.BeginInvokeOnMainThread(() =>
{
var (longitude, latitude) = (location.Longitude, location.Latitude).Wgs84ToGcj02();
var position = new Position(latitude, longitude);
var mapSpan = new MapSpan(position, 0.01, 0.01);
Content = new Map(mapSpan)
{
Pins =
{
new Pin
{
Label = bill.Name,
Type = PinType.Generic,
Position = position,
Address = bill.Store
}
}
};
});
}
}
}
private async Task<Location> GetCurrentLocation()
{
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(10));
tokenSource = new CancellationTokenSource();
var status = await Helper.CheckAndRequestPermissionAsync<Permissions.LocationWhenInUse>();
if (status != PermissionStatus.Granted)
{
return null;
}
return await Geolocation.GetLocationAsync(request, tokenSource.Token);
}
catch (FeatureNotSupportedException) { }
catch (FeatureNotEnabledException) { }
catch (PermissionException) { }
catch (Exception ex)
{
Helper.Error("location.get", ex);
}
tokenSource = null;
return null;
}
}
}