Finding latitude and longitude using Android.Xamar

2019-07-23 15:04发布

问题:

I am trying to develop a store locator app in Android.Xamarin. My first step is to find my location's latitude and longitude.

But my emulator/device screen shows nothing.

I have my uses-permissions set to <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

This is my code:-

Location _currentLocation;
    LocationManager _locationManager;
    TextView _locationText;
    TextView _addressText;
    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();
    }

    void InitializeLocationManager()
    {
        _locationManager = (LocationManager)GetSystemService(LocationService);
        Criteria criteriaForLocationService = new Criteria
        {
            Accuracy = Accuracy.Fine
        };
        IList<string> 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);
    }


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

        Geocoder geocoder = new Geocoder(this);
        IList<Address> addressList = await geocoder.GetFromLocationAsync(_currentLocation.Latitude, _currentLocation.Longitude, 10);

        Address address = addressList.FirstOrDefault();
        if (address != null)
        {
            StringBuilder deviceAddress = new StringBuilder();
            for (int i = 0; i < address.MaxAddressLineIndex; i++)
            {
                deviceAddress.Append(address.GetAddressLine(i))
                    .AppendLine(",");
            }
            _addressText.Text = deviceAddress.ToString();
        }
        else
        {
            _addressText.Text = "Unable to determine the address.";
        }
    }
    #region ILocationListener implementation
    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)
    {
    }
    #endregion

I get the output as the following:-

回答1:

If you are executing this code in emulator then keep this in mind that Emulator doesn't display GPS Information. And when you are trying in above code in real device, make sure, your GPS is turn on and you should be below open sky or near Window.