【问题标题】:Get user's current location in Xamarin iOS获取用户在 Xamarin iOS 中的当前位置
【发布时间】:2023-04-01 11:32:01
【问题描述】:

我想获取用户在 iOS Xamarin 中的当前位置。这是我的代码。

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.当我启动我的应用程序时,会弹出一个对话框“AppName 想使用您当前的位置”,一两秒后它会自行关闭(在我点击任何按钮之前)。为什么会这样?

2。 当我手动(设置->隐私->位置)为我的应用启用位置服务时,应用工作正常。我希望这以编程方式发生。我检查了this post 也类似question。我哪里出错了?

请帮忙

【问题讨论】:

    标签: ios xamarin.ios xamarin cllocationmanager


    【解决方案1】:

    这适用于 iOS8 吗?你打电话给 RequestWhenInUseAuthorization 了吗?

    至于对话框的关闭,这很可能是由于 locationManager 变量没有在类级别声明。

    【讨论】:

    • 感谢您的回复。这不适用于 iOS8,将 locationManager 移动到类级别并没有解决问题。对话框仅在 iOS7 上消失,在 iOS6 上工作正常!!奇怪!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2014-01-24
    • 2011-07-20
    • 2023-03-03
    相关资源
    最近更新 更多