Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody. I'm trying to get current location in my android application. It should be displayed in a textview but it doesn't. Can anybody say to me what I'm doing wrong?

What I have tried:

This is my code:
C#
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)
{

}


I have found out that the app is showing location only when the location provider is network but when the location provider is gps,the app is not showing location.
Can anybody tell me why this happens?
Posted
Updated 29-May-16 1:23am
v3
Comments
PeMaCN 31-May-16 16:10pm    
Does anyone know the answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900