1

i am trying to get the current location in android for that i have used the android and following is my code

locationManager = (LocationManager) getSystemService(this.context.LOCATION_SERVICE);
 Criteria crta = new Criteria();
     crta.setAccuracy(Criteria.ACCURACY_FINE);
     crta.setAltitudeRequired(true);
     crta.setBearingRequired(true);
     crta.setCostAllowed(true);
     crta.setPowerRequirement(Criteria.POWER_LOW); 
     String provider = locationManager.getBestProvider(crta, true);
     Log.d("","provider : "+provider);
     provider = LocationManager.GPS_PROVIDER; 
     Log.d("","provider : "+provider);
     locationManager.requestLocationUpdates(provider, 0, 0, (LocationListener) this);
     Location location = locationManager.getLastKnownLocation(provider); 
     //isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (location != null)
     {
       double  latitude = location.getLatitude();
       double  longitude = location.getLongitude();
       txtLat.setText("Latitude"+latitude+"longitude"+longitude);
       Log.d("not null", "location is not null");
     }
    else
    {

      Toast.makeText(getApplicationContext(), "locatyion is null", Toast.LENGTH_LONG).show();
      Log.d("null","location is null"); 
    }

The problem is that it doesnt gives me my current location when i open the application the location is null. Can anyone help ? i have seen many examples and tried others too but didnt work. I have added

if (!isNetworkEnabled)
    {
     Toast.makeText(getApplicationContext(), "No network provider the gps wont work", Toast.LENGTH_LONG).show();
        // no network provider is enabled
    }
    else{

        Toast.makeText(getApplicationContext(), "Network is available", Toast.LENGTH_LONG).show();
    }

and its says gps is available and network is not available

1 Answers1

0

Code need to be async with a broadcast receiver of location changed listener...

That happens because GPS needs some time to provide a location, when it starts for the first time everything is null.

see the code below:

public class Example extends Activity implements LocationListener {
    LocationManager mLocationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
            // Do something with the recent location fix 
            //  if it is less than two minutes old,
            //  otherwise wait for the update below
        }
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
            Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude());
            // You need to call this whenever you are done:
            // mLocationManager.removeUpdates(this);
        }
    }

    // Required functions    
    public void onProviderDisabled(String arg0) {}
    public void onProviderEnabled(String arg0) {}
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}

more here

Community
  • 1
  • 1
madlymad
  • 6,367
  • 6
  • 37
  • 68