Xamarin.forms GetPositionAsync在Xamarin Android中不起作用
这是我正在使用的代码。 这段代码可以在IOS上运行,但从未在Android上运行过。 我在他们的网站上看到了一些类似的问题,但没有得到任何帮助Xamarin.forms GetPositionAsync在Xamarin Android中不起作用,xamarin.forms,xamarin.android,geolocation,Xamarin.forms,Xamarin.android,Geolocation,这是我正在使用的代码。 这段代码可以在IOS上运行,但从未在Android上运行过。 我在他们的网站上看到了一些类似的问题,但没有得到任何帮助 public async Task<Position> GetCurrentLocation() { Position position = null; try { var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 100;
public async Task<Position> GetCurrentLocation()
{
Position position = null;
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100;
position = await locator.GetLastKnownLocationAsync();
if (position != null)
{
//got a cahched position, so let's use it.
return;
}
if(!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
{
//not available or enabled
return;
}
position = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);
}
catch (Exception ex)
{
//Display error as we have timed out or can't get location.
}
if(position == null)
return;
var output = string.Format("Time: {0} \nLat: {1} \nLong: {2} \nAltitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \nHeading: {6} \nSpeed: {7}",
position.Timestamp, position.Latitude, position.Longitude,
position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);
Debug.WriteLine(output);
}
公共异步任务GetCurrentLocation()
{
位置=空;
尝试
{
var定位器=CrossGeolocator.Current;
locator.DesiredAccuracy=100;
位置=等待定位器。GetLastKnownLocationAsync();
如果(位置!=null)
{
//有一个cahched位置,所以让我们使用它。
返回;
}
如果(!locator.IsGeolocationAvailable | |!locator.IsGeolocationEnabled)
{
//不可用或未启用
返回;
}
position=await locator.GetPositionAsync(TimeSpan.FromSeconds(20),null,true);
}
捕获(例外情况除外)
{
//显示错误,因为我们已超时或无法获取位置。
}
如果(位置==null)
返回;
var output=string.Format(“时间:{0}\nLat:{1}\nLong:{2}\nAltitude:{3}\nAltitude精度:{4}\n精度:{5}\n读取:{6}\n种子:{7}”,
位置,时间戳,位置,纬度,位置,经度,
位置。高度,位置。高度准确度,位置。准确度,位置。航向,位置。速度);
Debug.WriteLine(输出);
}
我总是得到任务取消的例外
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00026] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at Plugin.Geolocator.GeolocatorImplementation+<GetPositionAsync>d__33.MoveNext () [0x0042a] in C:\projects\geolocatorplugin\src\Geolocator.Plugin.Android\GeolocatorImplementation.cs:206
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0001a] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
at MtwPublicApp.Services.LocationService+<GetCurrentLocation>d__2.MoveNext ()
System.Threading.Tasks.TaskCanceledException:任务已取消。
在:0中的System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x00026]处
0中的System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[0x00028]处
0中的System.Runtime.CompilerServices.TaskWaiter.ValidateEnd(System.Threading.Tasks.Task任务)[0x00008]处
位于System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult()[0x00000]的位置:0
在C:\projects\geologitorplugin\src\geologitor.Plugin.Android\geologitorimplementation.cs:206中的Plugin.geologiator.geologitorImplementation+d_u33.MoveNext()[0x0042a]处
---来自引发异常的上一个位置的堆栈结束跟踪---
0中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]处
0中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x0001a]处
0中的System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[0x00028]处
0中的System.Runtime.CompilerServices.TaskWaiter.ValidateEnd(System.Threading.Tasks.Task任务)[0x00008]处
位于System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult()[0x00000]的位置:0
在mtwPublicCapp.Services.LocationService+d_u2.MoveNext()
请帮我修一下
谢谢 你检查权限了吗?当然。我正在使用Plugin.permissions在此之前您是否请求所有权限?在呼叫位置之前,您能检查一下您是否有权限吗?还有,你在测试什么?当然,它说授予位置权限你在运行什么?模拟器,模拟器,真实设备?添加超时是否得到您的位置?是否检查权限?当然。我正在使用Plugin.permissions在此之前您是否请求所有权限?在呼叫位置之前,您能检查一下您是否有权限吗?还有,你在测试什么?当然,它说授予位置权限你在运行什么?模拟器,模拟器,真实设备?添加超时是否得到您的位置?