Click here to Skip to main content
11,920,156 members (57,813 online)
Rate this:
Please Sign up or sign in to vote.
See more: Mobile Android
->Below Class is Find Current Location.

public class CurrentLocation {

private LocationManager locationManager;
private Context context;
private Location location;
private LocationListener locationListener;

public CurrentLocation(Context ctx) {
this.context = ctx;

locationManager = (LocationManager) context
List providers = locationManager.getAllProviders();

Criteria locationCritera = new Criteria();
String providerName = locationManager.getBestProvider(locationCritera,

location = locationManager.getLastKnownLocation(providerName);
locationListener = new MyLocationListener();

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
location = locationManager.getLastKnownLocation(providerName);

public Location getLocation() {
return location;

->Below Class is MyLocationListner.

public class MyLocationListener implements LocationListener {

public void onLocationChanged(Location loc) {

public void onProviderDisabled(String arg0) {

public void onProviderEnabled(String provider) {

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


->I Get Current Location.

CurrentLocation currLocation = new CurrentLocation(MainActivity.this);
Location location = currLocation.getLocation();

Toast.makeText(,"Lat "+location.getLatitude+" Long : "+location.getLongitude,

But I have Problem...
When I Install Application That Time give correct Location Latitude and Longitude,But when I
go to Onther city and run installed Application that time it give me previous Location ,mean Last Known Location latitude and Longitude.Insort my Location is Not Updated when Location Change.

please Tell me how update Location,or refresh last known location?
Posted 10-Jun-13 19:18pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

public void onLocationChanged(Location loc) {
What does it do? Nothing. Isn't that the line where you ought to react on the LocationChanged event?

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

  Print Answers RSS
0 OriginalGriff 455
1 phil.o 170
2 CPallini 150
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,153
3 George Jonsson 2,810
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web03 | 2.8.151120.1 | Last Updated 11 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100