简体   繁体   English

在Xamarin iOS中获取用户的当前位置

[英]Get user's current location in Xamarin iOS

I would like to get the user's current location in iOS Xamarin. 我想获取用户在iOS Xamarin中的当前位置。 Here is my code. 这是我的代码。

CLLocationManager locationManager = new CLLocationManager();
locationManager.StartUpdatingLocation();
locationManager.StartUpdatingHeading();

locationManager.LocationsUpdated += delegate(object sender , CLLocationsUpdatedEventArgs e )
{
    foreach(CLLocation loc in e.Locations)
    {
        Console.WriteLine(loc.Coordinate.Latitude);
    }
};

1.When i launch my app, a dialog "AppName would like to use your current location" pops in and after one or two seconds it dismisses by itself(before i could click any button). 1.当我启动我的应用程序时,将弹出一个对话框“ AppName想使用您的当前位置”,一到两秒钟后,它会自行关闭(在我可以单击任何按钮之前)。 Why is this happening? 为什么会这样呢?

2. App works fine when i manually(settings->privacy->locations) enable location services for my app. 2.当我手动(设置->隐私->位置)为我的应用启用位置服务时,应用运行良好。 I want this to happen programmatically. 我希望这可以通过编程实现。 I have checked this post also similar question .Where am i going wrong? 我已经检查了这个帖子也有类似的问题 。我哪里错了?

Please help 请帮忙

Is this for iOS8? 这是iOS8版吗? Did you call RequestWhenInUseAuthorization? 您是否致电RequestWhenInUseAuthorization?

As for the dismiss of the dialog, that is most likely due to the locationManager variable not being declared at the class level. 至于关闭对话框,这很可能是由于在类级别未声明locationManager变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM