在 xamarin.ios 上无法找到我的位置
finding my location doesn't work on xamarin.ios
在构建我的 xamarin 应用程序时,我注意到 xamarin.ios 上有一个相当奇怪的行为。我正在使用 geolocator.plugin 来查找我的位置,当然我会根据需要通过应用程序使用此位置。在 xamarin.android 项目中一切正常,但在 xamarin.ios 中,一旦我调用我找到位置的服务,就会停止。这是我的代码示例:
service.cs
public async Task<Plugin.Geolocator.Abstractions.Position> GetDeviceCurrentLocation()
{
try
{
var locator = Plugin.Geolocator.CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(3));
if (position != null)
{
return position;
}
}
catch (Exception ex)
{
Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
}
return new Plugin.Geolocator.Abstractions.Position();
}
这是我的 MyViewExample.xaml.cs
public partial class MyViewExample : ContentPage
{
public MyViewExample()
{
InitializeComponent();
Service cs = new Service();
var myLocation = Task.Run(() => api.GetDeviceCurrentLocation()).Result;
var myLatitude = myLocation.Latitude;
var myLongitude = myLocation.Longitude;
Debug.Writeline("Latitude is : " + myLatitude + " and Longitude is : " + myLongitude);
}
}
这是我的 info.plist 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
--
--
--
--
--
--
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access location when open and in the background</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Need location for geolocator plugin</string>
<key>RequestWhenInUseAuthorization</key>
<string>Need location for geolocator plugin</string>
<key>CFBundleIdentifier</key>
<string>my.app.development</string>
</dict>
</plist>
谁能告诉我为什么我在 xamarin.ios 上获取位置时遇到问题?我忘记了什么吗?我看过很多例子,但 none 对我有用。
关于如何解决这个问题的任何想法?任何帮助都是至关重要的,非常感谢:)。提前致谢!
因为我的任务是异步的,所以我需要 await
这个任务。但是我没有等待它,而是使用这行代码 运行 主线程上的任务。
var myLocation = Task.Run(() => cs.GetDeviceCurrentLocation()).Result;
这就是我的应用程序的问题。貌似xamarin.ios不支持这种"hack"。一旦我制作了另一个异步方法并使用了这一行
var myLocation = await cs.GetDeviceCurrentLocation();
一切都很顺利。希望它能帮助其他人寻找同样问题的答案:)
在构建我的 xamarin 应用程序时,我注意到 xamarin.ios 上有一个相当奇怪的行为。我正在使用 geolocator.plugin 来查找我的位置,当然我会根据需要通过应用程序使用此位置。在 xamarin.android 项目中一切正常,但在 xamarin.ios 中,一旦我调用我找到位置的服务,就会停止。这是我的代码示例:
service.cs
public async Task<Plugin.Geolocator.Abstractions.Position> GetDeviceCurrentLocation()
{
try
{
var locator = Plugin.Geolocator.CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(3));
if (position != null)
{
return position;
}
}
catch (Exception ex)
{
Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
}
return new Plugin.Geolocator.Abstractions.Position();
}
这是我的 MyViewExample.xaml.cs
public partial class MyViewExample : ContentPage
{
public MyViewExample()
{
InitializeComponent();
Service cs = new Service();
var myLocation = Task.Run(() => api.GetDeviceCurrentLocation()).Result;
var myLatitude = myLocation.Latitude;
var myLongitude = myLocation.Longitude;
Debug.Writeline("Latitude is : " + myLatitude + " and Longitude is : " + myLongitude);
}
}
这是我的 info.plist 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
--
--
--
--
--
--
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access location when open and in the background</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Need location for geolocator plugin</string>
<key>RequestWhenInUseAuthorization</key>
<string>Need location for geolocator plugin</string>
<key>CFBundleIdentifier</key>
<string>my.app.development</string>
</dict>
</plist>
谁能告诉我为什么我在 xamarin.ios 上获取位置时遇到问题?我忘记了什么吗?我看过很多例子,但 none 对我有用。 关于如何解决这个问题的任何想法?任何帮助都是至关重要的,非常感谢:)。提前致谢!
因为我的任务是异步的,所以我需要 await
这个任务。但是我没有等待它,而是使用这行代码 运行 主线程上的任务。
var myLocation = Task.Run(() => cs.GetDeviceCurrentLocation()).Result;
这就是我的应用程序的问题。貌似xamarin.ios不支持这种"hack"。一旦我制作了另一个异步方法并使用了这一行
var myLocation = await cs.GetDeviceCurrentLocation();
一切都很顺利。希望它能帮助其他人寻找同样问题的答案:)