如何在单色触控中使用位置?
我正在尝试使用以下代码,但任何事件都不会触发(AuthorizationChanged& LocationsUpdated)
我该怎么办?请告知
public Task<CLLocation> test()
{
TaskCompletionSource<CLLocation> objTaskCompletionSource1 = new TaskCompletionSource<CLLocation> ();
CLLocation currentLocation = null;
CLLocationManager LocMgr = new CLLocationManager ();
if (CLLocationManager.LocationServicesEnabled)
{
LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
{
currentLocation = e.Locations [e.Locations.Length - 1];
locationUpdated = false;
if (currentLocation != null && AllAreas != null)
{
LocationDetector.Instance.UpdateCurrentArea (new RLatLng (currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude));
objTaskCompletionSource1.SetResult(currentLocation);
}
else
{
currentLocation = null;
objTaskCompletionSource1.SetResult(currentLocation);
}
};
LocMgr.AuthorizationChanged+= (object sender, CLAuthorizationChangedEventArgs e) => {
Console.WriteLine("AuthorizationChanged Fired");
};
LocMgr.Failed += (object sender, NSErrorEventArgs e) =>
{
};
LocMgr.StartUpdatingLocation ();
}
else
{
currentLocation = null;
objTaskCompletionSource1.SetResult (currentLocation);
Console.WriteLine ("Location services not enabled, please enable this in your Settings");
}
return objTaskCompletionSource1.Task;
}
答案 0 :(得分:1)
您可能正在 iOS 8 上对此进行测试。
对于 iOS 8 ,您现在需要请求授权。
因此,在 ViewDidLoad 中使用以下内容(制作LocMgr类作用域级别 - 因此请删除版本中的本地实例): -
LocMgr = new CLLocationManager();
if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
LocMgr.RequestWhenInUseAuthorization();
}
此外,为了使上述功能正常工作,并且对话框显示您还需要将以下条目添加到 info.plist 中: -
<key>NSLocationWhenInUseUsageDescription</key>
<value>{some message that will be shown to the end-user}</value>
更新1: -
我正在使用的代码: -
在 ViewDidLoad 中: -
LocMgr = new CLLocationManager ();
if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
LocMgr.RequestWhenInUseAuthorization ();
}
UIButton objButton1 = new UIButton (UIButtonType.RoundedRect);
objButton1.SetTitle ("Click Me", UIControlState.Normal);
objButton1.TouchUpInside += (async (o2, e2) => {
CLLocation objLocationInfo = await Test1();
Console.WriteLine("Completed");
});
this.View = objButton1;
Test1功能: -
public Task<CLLocation> Test1()
{
TaskCompletionSource<CLLocation> objTaskCompletionSource1 = new TaskCompletionSource<CLLocation> ();
CLLocation currentLocation = null;
if (CLLocationManager.LocationServicesEnabled) {
LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => {
currentLocation = e.Locations [e.Locations.Length - 1];
locationUpdated = false;
//if (currentLocation != null && AllAreas != null) {
if (currentLocation != null) {
//LocationDetector.Instance.UpdateCurrentArea (new RLatLng (currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude));
objTaskCompletionSource1.SetResult (currentLocation);
} else {
currentLocation = null;
objTaskCompletionSource1.SetResult (currentLocation);
}
};
LocMgr.AuthorizationChanged += (object sender, CLAuthorizationChangedEventArgs e) => {
Console.WriteLine ("AuthorizationChanged Fired");
};
LocMgr.Failed += (object sender, NSErrorEventArgs e) => {
Console.WriteLine("AHH Failed");
};
LocMgr.StartUpdatingLocation ();
} else {
currentLocation = null;
objTaskCompletionSource1.SetResult (currentLocation);
Console.WriteLine ("Location services not enabled, please enable this in your Settings");
}
return objTaskCompletionSource1.Task;
}
您在课堂上也需要这些: -
private bool locationUpdated = false;
private CLLocation currentLocation = null;
private CLLocationManager LocMgr;
请记住,您还需要修改 info.plist 。
如果您运行示例在Console.WriteLine("Completed");
放置一个断点,那么您应该能够检查objLocationInfo
并看到它有一个位置。