Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find current location latitude and longitude in Android [duplicate]

Tags:

android

I want to find latitude and longitude of my current location in an android application. I just need it when I am stable with my phone.

My code is:

LocationManager locationManager;
    locationManager = (LocationManager)getSystemService
    (Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation
    (LocationManager.GPS_PROVIDER);


    locationManager.requestLocationUpdates(                
             LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
    updateWithNewLocation(location);
    }

    private void updateWithNewLocation(Location location) {
    TextView myLocationText = (TextView)findViewById(R.id.myLocationText);

    String latLongString;

    if (location != null) {
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
    latLongString = "No location found";
    }

    myLocationText.setText("Your Current Position is:\n" +
    latLongString);
    }

After running it on emulator i always find "no location found". Why this is so?

like image 700
rashmi Avatar asked Feb 12 '10 08:02

rashmi


1 Answers

public class GPSmap extends Activity {

    GeoPoint geoPoint;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocationManager locManager;
        setContentView(R.layout.main);
        locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
            500.0f, locationListener);
        Location location = locManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
        }
    }

    private void updateWithNewLocation(Location location) {
        TextView myLocationText = (TextView) findViewById(R.id.text);
        String latLongString = "";
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;
        } else {
            latLongString = "No location found";
        }
        myLocationText.setText("Your Current Position is:\n" + latLongString);
    }

    private final LocationListener locationListener = new LocationListener() {

        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {}

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

If you will use this code, you will get answer.

like image 58
JohnNick Avatar answered Oct 14 '22 15:10

JohnNick