3

I use this code for getting current location:

LocationManager lm = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);

/*
 * Loop over the array backwards, and if you get an accurate location,
 * then break out the loop
 */
Location l = null;

for (int i = providers.size() - 1; i >= 0; i--) {

    l = lm.getLastKnownLocation(providers.get(i));
    if (l != null)
        break;
}

if (l==null) {
    return;
}
Toast.makeText(this, String.valueOf(l.getLatitude()), Toast.LENGTH_LONG).show(); 

But I never see a latitude, because l is null always. My Manifest file contains ACCESS_FINE_LOCATION and INTERNET permissions. Where have I made mistake?

user1078760
  • 749
  • 2
  • 11
  • 17
  • 1
    Refer http://developer.android.com/guide/topics/location/obtaining-user-location.html. You need to add a location listener and request for updates. – blessanm86 Dec 07 '11 at 06:40
  • 1
    Why you use this code ? if not necesory to use it then use this: http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android and also refer the developer site here:http://developer.android.com/reference/android/location/LocationManager.html – Shreyash Mahajan Dec 07 '11 at 06:47
  • This may work for you i guess: http://stackoverflow.com/a/3145655/265167 – Yaqub Ahmad Dec 07 '11 at 06:51

4 Answers4

3

Look into this excellent explanation:

http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

At the moment you just ask some available providers for last known location, which and break on first answer. This could be not the provider you like.

Gili
  • 86,244
  • 97
  • 390
  • 689
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
0

Just call the method requestLocationUpdates of LocationManager, and implement the LocationListener to handle the location update.

http://developer.android.com/reference/android/location/LocationManager.html

Tang Ke
  • 1,508
  • 12
  • 12
0

I think best opensource project to implement location is foursquare on google code: http://code.google.com/p/foursquared/

Related code is in com.joelapenna.foursquared.loaction.BestLoactionListener

You can check out how does they deal with Loaction , and how to update current loaction,it help me a lot.

Wangchao0721
  • 909
  • 1
  • 9
  • 23
0

Following code works for me. I have used GPS as well Network Service to determine the current location of the device.

private void findCurrentLocation()
{
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (locationManager == null)
    {
        Toast.makeText(CityActivity.this, "Location Manager Not Available",
        Toast.LENGTH_SHORT).show();
        return;
    }
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location == null)
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location != null)
    {
        lat = location.getLatitude();
        lng = location.getLongitude();
        myFunction(location);
    }
    else
    {
        locationListener = new LocationListener()
        {
            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2)
            {;}

            @Override
            public void onProviderEnabled(String arg0)
            {;}

            @Override
            public void onProviderDisabled(String arg0)
            {;}

            @Override
            public void onLocationChanged(Location l)
            {
                location = l;
                locationManager.removeUpdates(this);
                if (l.getLatitude() == 0 || l.getLongitude() == 0)
                {;}
                else
                {
                    lat = l.getLatitude();
                    lng = l.getLongitude();
                    myFunction(location);
                }
            }
        }
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, f, locationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
        locationtimer = new CountDownTimer(30000, 5000)
        {
            @Override
            public void onTick(long millisUntilFinished)
            {
                if (location != null)
                    locationtimer.cancel();
            }

            @Override
            public void onFinish()
            {
                if (location == null)
                {;}
            }
        };
        locationtimer.start();
    }
}
Shabbir Panjesha
  • 1,851
  • 5
  • 20
  • 29