【发布时间】:2018-01-29 13:23:09
【问题描述】:
在构建我的 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 上获取位置信息?我是不是忘记了什么?我经历了很多例子,但没有一个对我有用。 关于如何解决这个问题的任何想法?任何帮助都将是至关重要的并受到高度赞赏:)。提前致谢!
【问题讨论】:
-
你尝试增加
DesiredAccuracy吗?也许您的 iOS 设备不够准确? -
其实没有,我没试过。我现在就试试。 xamarin.ios 应用程序总是卡在这一行
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(3));。你在使用这个插件时遇到过什么问题吗? @ChrFin -
我没有使用它,我只是猜测,因为这可能是问题...
-
您使用的是 iOS 模拟器还是物理设备?
-
我实际上都在使用。它在他们俩都不起作用:/。当应用程序第一次启动时,它甚至不要求位置权限。有什么想法吗?
标签: c# xamarin xamarin.forms xamarin.ios geolocation