0

首先,我使用了本教程

得到纬度和经度,但我什么也没得到,所以这是我的代码:

[Activity(Label = "GetLocation", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity, ILocationListener
{
    private Location _currentLocation;
    private LocationManager _locationManager;
    private TextView _locationText;
    private TextView _addressText;
    private string _locationProvider;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);

        _addressText = FindViewById<TextView>(Resource.Id.address_text);
        _locationText = FindViewById<TextView>(Resource.Id.location_text);
        FindViewById<TextView>(Resource.Id.get_address_button).Click += AddressButton_OnClick;

        InitializeLocationManager();
    }

    private void InitializeLocationManager()
    {
        _locationManager = (LocationManager)GetSystemService(LocationService);
        var criteriaForLocationService = new Criteria
        {
            Accuracy = Accuracy.Fine
        };
        var acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);

        if (acceptableLocationProviders.Any())
        {
            _locationProvider = acceptableLocationProviders.First();
        }
        else
        {
            _locationProvider = String.Empty;
        }
    }

    protected override void OnResume()
    {
        base.OnResume();
        _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
    }

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

    private void AddressButton_OnClick(object sender, EventArgs eventArgs)
    {
        if (_currentLocation == null)
        {
            _addressText.Text = "Can't determine the current location.";
            return;
        }

        new Thread(() =>
        {
            var addressText = "Unable to find a location.";
            var geocoder = new Geocoder(this);
            var addressList = geocoder.GetFromLocation(_currentLocation.Latitude, _currentLocation.Longitude, 50);
            var address = addressList.FirstOrDefault();

            if (address != null)
            {
                var deviceLocation = new StringBuilder();
                for (var i = 0; i < address.MaxAddressLineIndex; i++)
                {
                    deviceLocation.Append(address.GetAddressLine(i))
                        .AppendLine(",");
                }
                _addressText.Text = deviceLocation.ToString();
            }
            RunOnUiThread(() => { _addressText.Text = addressText; });
        }).Start();
    }

    public void OnLocationChanged(Location location)
    {
        _currentLocation = location;
        if (_currentLocation == null)
        {
            _locationText.Text = "Unable to determine your location.";
        }
        else
        {
            _locationText.Text = String.Format("{0},{1}", _currentLocation.Latitude, _currentLocation.Longitude);
        }
    }

    public void OnProviderDisabled(string provider) { }

    public void OnProviderEnabled(string provider) { }

    public void OnStatusChanged(string provider, Availability status, Bundle extras) { }

}

因此,如果有人对我的代码有什么问题有任何想法,我将不胜感激。

4

1 回答 1

0

您的代码中有一个地方是您_addressText.Text从后台线程更新 UI。这也可以解释为什么当您单击按钮时,您没有看到任何地址更新。请参阅下面的一行代码片段:

if (address != null)
{
    var deviceLocation = new StringBuilder();
    for (var i = 0; i < address.MaxAddressLineIndex; i++)
    {
        deviceLocation.Append(address.GetAddressLine(i))
            .AppendLine(",");
    }

    // Here you were updating the UI thread from the background:
    RunOnUiThread(() => _addressText.Text = deviceLocation.ToString());
}
于 2013-05-31T13:32:58.090 回答