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