Ios Monotouch:获取用户当前lcoation
如何在monotouch中获取使用位置? 我正在尝试使用以下代码,但任何事件都不会触发(AuthorizationChanged&LocationsUpdated)Ios Monotouch:获取用户当前lcoation,ios,xamarin.ios,xamarin,Ios,Xamarin.ios,Xamarin,如何在monotouch中获取使用位置? 我正在尝试使用以下代码,但任何事件都不会触发(AuthorizationChanged&LocationsUpdated) 我该怎么办?请建议 public Task<CLLocation> test() { TaskCompletionSource<CLLocation> objTaskCompletionSource1 = new TaskCompletionSource<CLLocation> ();
我该怎么办?请建议
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;
}
公共任务测试()
{
TaskCompletionSource objTaskCompletionSource1=新TaskCompletionSource();
CLLocation currentLocation=null;
CLLocationManager LocMgr=新的CLLocationManager();
如果(CLLocationManager.LocationServicesEnabled)
{
LocMgr.LocationsUpdated+=(对象发送方,CLLocationsUpdatedEventArgs e)=>
{
currentLocation=e.位置[e.位置.长度-1];
位置更新=错误;
if(currentLocation!=null&&allreas!=null)
{
LocationDetector.Instance.UpdateCurrentArea(新的RLatLng(currentLocation.Coordinate.Latitude,currentLocation.Coordinate.Longitude));
objTaskCompletionSource1.SetResult(当前位置);
}
其他的
{
currentLocation=null;
objTaskCompletionSource1.SetResult(当前位置);
}
};
LocaMgr.AuthorizationChanged+=(对象发送方,CLAuthorizationChangedEventArgs e)=>{
Console.WriteLine(“授权已更改”);
};
LocMgr.Failed+=(对象发送方,NSErrorEventArgs e)=>
{
};
LocMgr.StartUpdatingLocation();
}
其他的
{
currentLocation=null;
objTaskCompletionSource1.SetResult(当前位置);
Console.WriteLine(“未启用位置服务,请在您的设置中启用此功能”);
}
返回objTaskCompletionSource1.Task;
}
您可能正在iOS 8上测试此功能
对于iOS 8,您现在需要请求授权
因此,在您的ViewDidLoad中使用类似以下内容(使LocMgr类作用域级别-因此在您的版本中删除本地实例):-
此外,为了使上述内容起作用,并使对话框显示,您还需要在info.plist中添加以下条目:-
<key>NSLocationWhenInUseUsageDescription</key>
<value>{some message that will be shown to the end-user}</value>
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;
}
请记住,您还需要编辑您的info.plist
如果然后运行该示例,请在
Console.WriteLine(“Completed”)上放置断点
然后您应该能够检查objLocationInfo
并查看它是否有位置。我将LocMgr更改为全局,但事件不会触发:(@Chris如果您发送电子邮件,我会将您修改过的版本发送给您。它只包括我在上面所做的更改,但在iOS 8上对我有效。您能添加检索用户位置的完整代码吗?它显示对话框,但我的事件不会触发Hanks,我正在尝试使用您的代码,我可以使用我的类con吗拖拉机而不是viewDidLoad?因为我有单独的非视图类
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;