0

Possible Duplicate:
How do I get the current GPS location programmatically in Android?

Got a code below.. Will this get me my own location..?

public class MyLocationListener implements LocationListener {  

public static double latitude;  
public static double longitude;  

public void onLocationChanged(Location loc)  
{  
    loc.getLatitude();  
    loc.getLongitude();  
    latitude=loc.getLatitude();  
    longitude=loc.getLongitude();  
}  

public void onProviderDisabled(String provider)  
{  
    //print "Currently GPS is Disabled";  
}  
public void onProviderEnabled(String provider)  
{  
    //print "GPS got Enabled";  
}  
public void onStatusChanged(String provider, int status, Bundle extras)  
{  
}  
}  

I know this has been asked several times.. Please help me with the code...

Community
  • 1
  • 1
  • `Will this get me my own location..?` Why don't you try? – Jasper Apr 23 '12 at 10:19
  • Yes it will but still you need to do lot of things http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android – Sandip Jadhav Apr 23 '12 at 10:23
  • What @Jasper said. If this class is your only location code it won't work. Also, you know it has been asked several times. I'm pretty sure you can get a working example of location retrieving right here in SO. – jcxavier Apr 23 '12 at 10:23

1 Answers1

0

You may use this code to start listing for GPS updates and do something with the data you receive. It also prints messages in the log file for easy debugging. Please, note, this function does not return any results, it only starts listening to GPS. Results will be provided some time later in // do something here with the new location data part, when you may save them somewhere.

private void getGpsData() {
    locationManager = (LocationManager) owner.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.i(TAG, "location change:" + location.toString());

            String longitude = "Londitude: " + location.getLongitude();
            String latitude = "Latitude: " + location.getLatitude();

            if( location.hasAccuracy() ) {  // good enough?
                // do something here with the new location data
                ....
                //
                Log.i(TAG, "GPS listener done");
                locationManager.removeUpdates(this);    // don't forget this to save battery
            }
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.i(TAG, provider + " status:" + status);
        }

        public void onProviderEnabled(String provider) {
            Log.i(TAG, provider + " enabled");
        }

        public void onProviderDisabled(String provider) {
            Log.i(TAG, provider + " disabled");
        }
    };
    Log.i(TAG,"GPS listener started");
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener );
}
lenik
  • 23,228
  • 4
  • 34
  • 43
  • Its giving error in all the TAG..? – Radha Kumar Apr 24 '12 at 05:15
  • sorry, it's a common practice to include in every `Activity` class the following member: `private final static String TAG = "MyActivityClass";`, so you may see in the log, which class wrote which information. – lenik Apr 24 '12 at 06:47