Xamarin android在位置更改之前获取位置

时间:2017-09-11 16:10:30

标签: c# android xamarin

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Locations;
using Android.Content;

[Activity(Label = "Getlocation", MainLauncher = true)]
public class MainActivity : Activity, ILocationListener
{
    Button bttnGo;
    TextView txtLoc;
    LocationManager locMgr;
    Location currentLocation;
    string provider; 

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        bttnGo = FindViewById<Button>(Resource.Id.get_address_button);
        txtLoc = FindViewById<TextView>(Resource.Id.location_text);
        InitializeLocationManager();

        bttnGo.Click += BttnGo_Click;
    }

    async void BttnGo_Click(object sender, EventArgs e)
    {
        if (currentLocation == null)
        {
            txtLoc.Text = "No location found, try moving around";
        }
        else
        {
            txtLoc.Text = currentLocation.ToString();
        }
     }

    private void InitializeLocationManager()
    {

        locMgr = (LocationManager)GetSystemService(LocationService);

        Criteria criteriaForLocationService = new Criteria
        {
            Accuracy = Accuracy.Fine
        };

        IList<string> acceptableLocationProviders = locMgr.GetProviders(criteriaForLocationService, true);

        if(acceptableLocationProviders.Any())
        {
            provider = acceptableLocationProviders.First();
        }
        else
        {
            provider = string.Empty;
        }
    }


    protected override void OnResume()
    {
        base.OnResume();
        locMgr.RequestLocationUpdates(provider, 2000, 0, this);

    }

    protected override void OnPause()
    {
        base.OnPause();
        locMgr.RemoveUpdates(this);
    }

    public void OnLocationChanged(Location location)
    {

            currentLocation = location;
            if (currentLocation == null)
            {
                txtLoc.Text = "No location detected";

            }
            else
            {
                txtLoc.Text = location.Latitude.ToString();
            }

    }

    public void OnProviderDisabled(string provider)
    {

    }

    public void OnProviderEnabled(string provider)
    {

    }

    public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
    {

    }
}
}

我的编码知识很少,我只是按照教程检索当前位置。

我的问题是我只能在实际位置发生变化时检索位置。我知道这可能是因为currentLocation在OnLocationChanged发生之前没有得到值。怎么绕过这个?

1 个答案:

答案 0 :(得分:0)

  

Xamarin android在位置更改之前获取位置

您可以使用GetLastKnownLocation(provider)方法来实现此功能。这是一个简单的演示:

LocationManager locationManager = (LocationManager)GetSystemService(Context.LocationService);

IList<String> lp = locationManager.AllProviders;
foreach (String item in lp)
{
    Log.Debug("TAG", "Available Location Service :" + item);
}

//A class indicating the application criteria for selecting a location provider.
Criteria criteria = new Criteria();
//Indicates whether the provider is allowed to incur monetary cost.
criteria.CostAllowed = false;
//Set desired accuracy of location Accuracy
criteria.Accuracy = Accuracy.Coarse; 

//Returns the name of the provider that best meets the given criteria                 
String providerName = locationManager.GetBestProvider(criteria, true);
Log.Debug("8023", "------Location Service:" + providerName);

//Directly choose a Location Provider
//String providerName = LocationManager.GpsProvider;

Location location = locationManager.GetLastKnownLocation(providerName);

double lat = location.Latitude;
double lng = location.Longitude;

txtLoc.Text = "Latitude = " + lat + ", Longitude = " + lng;