Devrath
Devrath

Reputation: 42824

How to get the new latitude and longitude cordinates from android

How i am getting location details:

AppLocationService appLocationService = new AppLocationService(getApplicationContext());

Location nwLocation= appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
nwLocation.getLatitude();
nwLocation.getLongitude();

What is happening since my code has llast known location .... its giving me the last location that was updated say now its evening. its giving me the location updated during morning

What i want:: how can i make a fresh network request to get the current location at my position


AppLocationService.java

public class AppLocationService extends Service implements LocationListener {

    protected LocationManager locationManager;
    Location location;

    private static final long MIN_DISTANCE_FOR_UPDATE = 0;
    private static final long MIN_TIME_FOR_UPDATE = 0;

    public AppLocationService(Context context) {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    }

    public Location getLocation(String provider) {
        if (locationManager.isProviderEnabled(provider)) {
            locationManager.requestLocationUpdates(provider,
                    MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider);
                return location;
            }
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}

Upvotes: 1

Views: 677

Answers (4)

Ahd Radwan
Ahd Radwan

Reputation: 1100

Use onLocationChanged method and get the longitude and latitude from the parametr location

Code:

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = new Location(location);
    latitude=mCurrentLocation .getLatitude();
    longitude=mCurrentLocation .getLongitude();
}

Upvotes: 0

Jamil
Jamil

Reputation: 5535

How to call Explicitly onLocationChanged

public void locationlist()
     {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,
                    50, 0, this);
            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);

            if (location != null) {
              System.out.println("Provider " + provider + " has been selected.");
              onLocationChanged(location);

            } else {}
         if(location == null)
         {

         }
         else{
             lat = (double) (location.getLatitude());
             lng = (double) (location.getLongitude());
         }

     }

now call the method locationlist where you want like in a timer.

Upvotes: 0

azertiti
azertiti

Reputation: 3150

Your code is registering to receive location updates and also returns the last known location. What happens is expected because you didn't wait to receive an updated one.

Once the method "onLocationChanged" was called by the system you'll have the new one. You can then send a broadcast from the service.

You are trying to get the current location in a synchronous mode which is not possible as it takes some time to lookup the updated one.

Upvotes: 1

Badrul
Badrul

Reputation: 1642

The primary reason why you aren't getting updated location information quickly is that you're relying on the NETWORK_PROVIDER.

You should instead use this

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Upvotes: 0

Related Questions