Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Get GPS Coordinates and convert to address

Xammy10Xammy10 Member ✭✭✭

Pls help, I'm having a problem in terms of getting the GPS coordinate

I'm following this link but the GPS Coordinate is not working.
https://developer.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/

IDK why, but the codes seems correct for me

Thanks in advance :)

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
    edited March 2018

    Hello @Enohara
    have you got any error ...put a break point and see what's happens.
    what is it the problem ..? convert the coordinates to address OR to have the coordinate ?

  • Xammy10Xammy10 Member ✭✭✭
    edited March 2018

    @ChristopheBERNARD , I follow every step but when i try to run the application, nothing happens. Do i need to add some Nuget or any plugins?

    THIS IS MY MAIN ACTIVITY
    {
    [Activity(Label = "GetCurrentLocation", MainLauncher = true)]
    public class MainActivity : Activity, ILocationListener
    {
    static readonly string TAG = "X:" + typeof (MainActivity).Name;
    TextView addressText;
    Location currentLocation;
    LocationManager locationManager;

        string locationProvider;
        TextView locationText;
    
        public async void OnLocationChanged(Location location)
        {
            currentLocation = location;
            if (currentLocation == null)
            {
                locationText.Text = "LOPLOPLOPLOPOLPOLPOL";
            }
            else
            {
                locationText.Text = string.Format("{0:f6},{1:f6}", currentLocation.Latitude, currentLocation.Longitude);
                Address address = await ReverseGeocodeCurrentLocation();
                DisplayAddress(address);
            }
        }
    
        public void OnProviderDisabled(string provider)
        {
        }
    
        public void OnProviderEnabled(string provider)
        {
        }
    
        public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
        {
        }
    
    
    
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            addressText = FindViewById<TextView>(Resource.Id.txtAddress);
            locationText = FindViewById<TextView>(Resource.Id.txtCoordinate);
    
            FindViewById<Button>(Resource.Id.getAdd).Click += AddressButton_OnClick;
    
            InitializeLocationManager();
        }
    
        async void AddressButton_OnClick(object sender, EventArgs e)
        {
            if (currentLocation == null)
            {
                addressText.Text = "Can't determine the current address. Try again in a few minutes";
                return;
            }
            Address address = await ReverseGeocodeCurrentLocation();
            DisplayAddress(address);
        }
    
        async Task<Address> ReverseGeocodeCurrentLocation()
        {
            Geocoder geo = new Geocoder(this);
            IList<Address> addressList =
                await geo.GetFromLocationAsync(currentLocation.Latitude, currentLocation.Longitude, 10);
    
            Address address = addressList.FirstOrDefault();
            return address;
        }
    
        private void DisplayAddress(Address address)
        {
            if (address != null)
            {
                StringBuilder deviceAddress = new StringBuilder();
                for (int i = 0; i < address.MaxAddressLineIndex; i++)
                {
                    deviceAddress.AppendLine(address.GetAddressLine(i));
                }
                addressText.Text = deviceAddress.ToString();
            }
            else
            {
                addressText.Text = "LOPLOPLOP";
            }
        }
        private void InitializeLocationManager()
        {
            locationManager = (LocationManager)GetSystemService(LocationService);
            Criteria criteria = new Criteria
            {
                Accuracy = Accuracy.Fine
            };
            IList<string> acceptableProviders = locationManager.GetProviders(criteria, true);
    
            if (acceptableProviders.Any())
            {
                locationProvider = acceptableProviders.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);
        }
    }
    

    But just as I said, i follow every steps so it's just the same.

    Thanks in advance :)

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    @Enohara have you set the permission in the manifest ?
    android.permission.ACCESS_FINE_LOCATION
    android.permission.ACCESS_COARSE_LOCATION

    code seems to be nice

  • Xammy10Xammy10 Member ✭✭✭
    edited March 2018

    @ChristopheBERNARD said:
    @Enohara have you set the permission in the manifest ?
    android.permission.ACCESS_FINE_LOCATION
    android.permission.ACCESS_COARSE_LOCATION

    code seems to be nice

    Already did sir but still no result

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    @Enohara
    i suppose you have a result as : current location is null and
    addressText.Text = "Can't determine the current address. Try again in a few minutes"; ???

    move with the device and i suppose the gps is actif ?

  • Xammy10Xammy10 Member ✭✭✭

    I try moving but still, there's noting to show for coordinates

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
    edited March 2018

    i can't understand normally all worked i have a few sample i will look inside and i come again

  • Xammy10Xammy10 Member ✭✭✭

    @ChristopheBERNARD said:
    i can't understand normally all worked i have a few sample i will look inside and i come again

    Can i have those sample sir?

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    walk with the device and wait it's take some time before work ..
    you code is good i'm sure i have open my few sample and it's worked but few minutes before to have lat long

Sign In or Register to comment.