【发布时间】:2014-09-02 09:18:42
【问题描述】:
因此,在最终了解 Xamarin.Forms DependencyService 之后,我几乎可以让它返回设备的当前位置。
我的界面
public interface ICurrentLocation
{
MyLocation SetCurrentLocation();
}
我的位置
public class MyLocation
{
public double Latitude {get; set;}
public double Longitude{get; set;}
}
调用它的行
MyLocation location = DependencyService.Get<ICurrentLocation>().SetCurrentLocation();
以及在实现 Xamarin.Mobile 的 Geolocation 类的 Android 项目的 CurrentLocation 类中
[assembly: Dependency(typeof(CurrentLocation))]
namespace MyCockburn.Droid
{
public class CurrentLocation : Activity, ICurrentLocation
Geolocator locator;
Position position = new Position();
MyLocation location;
public MyLocation SetCurrentLocation()
{
GetPosition();
location = new MyLocation()
{
Latitude = position.Latitude,
Longitude = position.Longitude
};
return location;
}
async void GetPosition()
{
try
{
locator = new Geolocator(this) { DesiredAccuracy = 50 };
if (locator.IsListening != true)
locator.StartListening(minTime: 1000, minDistance: 0);
position = await locator.GetPositionAsync(timeout: 20000);
}
catch (Exception e)
{
Log.Debug("GeolocatorError", e.ToString());
}
}
}
我的问题似乎是在位置保持经度和纬度之前返回位置
我希望我的错误是显而易见的
编辑:如果我将代码作为普通的 Android Activity 运行,则该代码有效
【问题讨论】:
-
我在“地理定位器”声明中遇到错误。我添加了 nuget 包“Plugin.Geolocator”。
标签: android location xamarin xamarin.forms