Sjo*_*uit 5 c# android xamarin xamarin.forms xamarin.essentials
因此,我想获取用户位置并对其进行反向地理编码,以获取地址和街道号码。
因此,我安装了预发行版的Xamarin.Essentials软件包。我想使用地理定位和地理编码API,所以我写了一些代码,但是if (location != null)在尝试使用GetCurrentLocation时,这些代码永远无法到达。
我认为Xamarin.Essentials是否基于Xam.Plugin.Geolocator作者James Montemagno所说的预发行版并不重要:
我将继续工作和维护我的插件,但我建议您结帐Xamarin.Essentials,以查看它是否适合您的应用程序,因为它适用于我的所有应用程序!
我已搜索有关我遇到的问题的StackOverflow问题,但据我所知,找不到我能帮助我的问题。
我看过的问题:
我还尝试了添加超时是否可行: var request = new GeolocationRequest(GeolocationAccuracy.Medium, timeout: TimeSpan.MaxValue );
但是我在位置上也遇到了同样的问题。它从未到达if语句。
FillInLocationInForm(); 是在我的Page构造函数中设置的。
public Location location;并且public Placemark placemark;在Page类中设置。
这些是Page类中的相关功能:
public async void FillInLocationInForm()
{
if (await GetCurrentLocation())
{
if (await ReverseGeocodeLocation())
{
await this.DisplayAlert("Location", placemark.AdminArea + " " + placemark.FeatureName + " " + placemark.Locality + " " + placemark.SubLocality + " " + placemark.SubAdminArea + " " + placemark.SubThoroughfare + " " + placemark.Thoroughfare, "Okay", "Okay");
}
}
}
public async Task<bool> GetCurrentLocation ()
{
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
if (location != null)
{
this.location = location;
return await Task.FromResult(true);
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
}
catch (PermissionException pEx)
{
// Handle permission exception
}
catch (Exception ex)
{
// Unable to get location
}
return await Task.FromResult(false);
}
public async Task<bool> ReverseGeocodeLocation()
{
try
{
var lat = location.Latitude;
var lon = location.Longitude;
var placemarks = await Geocoding.GetPlacemarksAsync(lat, lon);
var placemark = placemarks?.FirstOrDefault();
if (placemark != null)
{
//var geocodeAddress =
// $"AdminArea: {placemark.AdminArea}\n" +
// $"CountryCode: {placemark.CountryCode}\n" +
// $"CountryName: {placemark.CountryName}\n" +
// $"FeatureName: {placemark.FeatureName}\n" +
// $"Locality: {placemark.Locality}\n" +
// $"PostalCode: {placemark.PostalCode}\n" +
// $"SubAdminArea: {placemark.SubAdminArea}\n" +
// $"SubLocality: {placemark.SubLocality}\n" +
// $"SubThoroughfare: {placemark.SubThoroughfare}\n" +
// $"Thoroughfare: {placemark.Thoroughfare}\n";
//Console.WriteLine(geocodeAddress);
this.placemark = placemark;
return await Task.FromResult(true);
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Feature not supported on device
}
catch (Exception ex)
{
// Handle exception that may have occurred in geocoding
}
return await Task.FromResult(false);
}
Run Code Online (Sandbox Code Playgroud)
我还将这些权限添加到我的应用程序AndroidManifest中,并在运行时请求它们。我已经在我的应用设置中看到了位置和存储。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
我有一种预感,我需要向中添加取消令牌GetLocationAsync,但是我还不知道如何。我在GeolocationRequest旁边看不到它:
我添加了CancellationToken,但仍未到达if语句。也许我现在还没有使用它。
using System.Threading; // add above
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken cancelToken = source.Token;
var location = await Geolocation.GetLocationAsync(request, cancelToken);
Run Code Online (Sandbox Code Playgroud)
下图中的断点从不触发,这意味着从不到达if语句。代码以某种方式脱离尝试,但没有进入任何catch语句中。我Console.WriteLine()现在在catch语句中使用a 。我的设备日志中什么也看不到。我测试了Console.WriteLine()在设备日志中看到的异常。

显然,我没有等待足够长的时间。就像杰森说的那样。
我第一次尝试添加超时是:
new GeolocationRequest(GeolocationAccuracy.Medium, timeout: TimeSpan.MaxValue);
Run Code Online (Sandbox Code Playgroud)
不知何故,上面的行不起作用。
无论如何,加上10秒的时间间隔给了我一个位置:
var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3478 次 |
| 最近记录: |