Forum Xamarin Xamarin.Android

Current location not showing

MarinPekoMarinPeko HRMember ✭✭

Hi everybody. I'm trying to get current location in my application. It should be displayed in a textview but it doesn't. Can anybody say to me what I'm doing wrong? This is my code:

private TextView locationText;
private Location currentLocation;
private LocationManager locationManager;
private string locationProvider;

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

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

     locationText = FindViewById<TextView>(Resource.Id.location);
     locationManager = (LocationManager)GetSystemService(LocationService);
     Criteria locationServiceCriteria = new Criteria
     {
           Accuracy = Accuracy.Coarse
     };

     IList<string> acceptableLocationProviders = locationManager.GetProviders(locationServiceCriteria,true);

     if (acceptableLocationProviders.Any())
     {
           locationProvider = acceptableLocationProviders.First();
     }
     else
     {
           locationText.Text = "No location provider!";
     }

}

protected override void OnResume()
{
base.OnResume();
locationManager.RequestLocationUpdates(locationProvider,0,0,this);
}

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

public void OnLocationChanged(Location location)
{
currentLocation = location;
if (location == null)
{
locationText.Text = "Location not found!";
}
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, [GeneratedEnum] Availability status, Bundle extras)
{

}

Answers

  • MarinPekoMarinPeko HRMember ✭✭

    I have also tried to run my app on a real device but the current location is not showing either. On the other hand, google maps app that I have installed on my phone finds my location with no problem at all. Please help me

  • MarinPekoMarinPeko HRMember ✭✭

    I followed each and every step from that tutorial. However, I have found out that my app is showing location when location provider is network and when location provider is gps the app is not showing location.
    Can anybody tell me why this happens?

  • SHIVANKUCHAUHANSHIVANKUCHAUHAN USMember ✭✭✭
    edited May 2016

    @MarinPeko : Are you using ACCESS_FINE_LOCATION in mainfest ? :
    __ To get location using GPS

    To get location using NetworkProvider
    __

    Additionally you need to use:smile: Accuracy = Accuracy.Fine instead of Accuracy = Accuracy.Coarse

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

    if (acceptableLocationProviders.Any())
    {
        _locationProvider = acceptableLocationProviders.First();
    }
    else
    {
        _locationProvider = string.Empty;
    }
    Log.Debug(TAG, "Using " + _locationProvider + ".");
    

    }

  • MarinPekoMarinPeko HRMember ✭✭

    I added what you said but still while using gps provider location is not showing.

Sign In or Register to comment.