簡體   English   中英

獲取用戶的當前位置Xamarin iOS

[英]Get user’s current location Xamarin iOS

我正在嘗試獲取位置。 我實現了所有內容,但沒有任何效果。

這是我的課:

public class Best2 : CLLocationManagerDelegate
{
    public CLLocationManager _locationManager;

    private Int32 _numEvento;

    public Best2()
    {
    }

    protected Best2(NSObjectFlag t) : base(t)
    {
    }

    protected internal Best2(IntPtr handle) : base(handle)
    {
    }

    public Best2(Int32 paramEvento)
    {
        _numEvento = paramEvento;

        _locationManager = new CLLocationManager ();
        _locationManager.PausesLocationUpdatesAutomatically = false;
        _locationManager.DesiredAccuracy = CLLocation.AccurracyBestForNavigation;

        if (UIDevice.CurrentDevice.CheckSystemVersion (9, 0)) {
            _locationManager.AllowsBackgroundLocationUpdates = true;
        }

        _locationManager.Delegate = this;

        StartLocationUpdates();
    }

    public void StartLocationUpdates ()
    {
        if (CLLocationManager.LocationServicesEnabled) {
            _locationManager.RequestLocation();

            System.Diagnostics.Debug.WriteLine(
                String.Format(
                    "FA&)#2 RequestLocation() Data:D {0}"
                    , DateTime.UtcNow
                )
            );

        }
    }

    public override void Failed (CLLocationManager manager, Foundation.NSError error)
    {
        System.Diagnostics.Debug.WriteLine(
            String.Format(
                "FA&)#2 Failed() Data:D {0}"
                , DateTime.UtcNow
            )
        );
    }

    public override void UpdatedLocation (CLLocationManager manager, CLLocation newLocation, CLLocation oldLocation)
    {
        System.Diagnostics.Debug.WriteLine(
            String.Format(
                "FA&)#2 UpdatedLocation() Data:D {0}"
                , DateTime.UtcNow
            )
        );
    }

我這樣稱呼:

        Best2 position = new Best2(1);

基於apple 文檔 ,我認為它應該調用方法Failed(如果無效)和UpdatedLocation(有效)。 但是什么都沒有呼喚。

我已經嘗試過讓類接收事件,但是它也不起作用。

根據有關“ 請求位置”的文檔,我們知道,如果要在iOS中使用位置,則必須遵循以下步驟:

  1. NSLocationWhenInUseUsageDescription鍵和NSLocationAlwaysAndWhenInUseUsageDescription鍵添加到Info.plist文件中。
  2. 如果您的應用程序支持iOS 10及更低版本,則將NSLocationAlwaysUsageDescription密鑰添加到Info.plist文件中。

然后在RequestLocation()之前,我們需要調用RequestAlwaysAuthorization()RequestWhenInUseAuthorization()來獲取用戶的身份驗證。

最后我們可以找到位置,但是您誤會了該事件:

我認為它應該調用方法Failed(如果不起作用)和UpdatedLocation(如果起作用)。

如果它起作用,則LocationsUpdated()事件將不會UpdatedLocation()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM