Question

Here is what I need to do. I need to launch my application and on the click of a button, I need to display the current coordinates, that is latitude and longitude. I followed this tutorial and used the following code for my purpose:

public class MainActivity extends Activity {

    public double latitude;
    public double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        final TextView latValueLabel = (TextView)findViewById(R.id.latLabel);
        final TextView lonValueLabel = (TextView)findViewById(R.id.lonLabel);
        Button setButton = (Button)findViewById(R.id.set_button);
        setButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub              
                latValueLabel.setText(String.valueOf(latitude));
                lonValueLabel.setText(String.valueOf(longitude));
            }
        });
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if(location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }

(Copy pasted only a part of the code, please ignore any unclosed brackets or anything like that.)

It continuously gets the latitude longitude as location changes and stores it to two double variables latitude and longitude and when the setButton is clicked, it displays the last stored lat-lon value. That would be the user's current location. Now the issue is, I launched the app and while still staying on the exact location from which the app is launched, I clicked the Set Button. But at that time the location is not changed, so the latitude and longitude are displayed as zero, which is the default value of the double variables. I need to take a walk around with the device so that the location is changed before I can get my actual coordinates. How can I get the lat-lon as soon as the app is launched?

Was it helpful?

Solution

You can use getLastKnownLocation(...) to initialise the longitude and latitude values like this:

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

double longitude = location.getLongitude();
double latitude = location.getLatitude();

OTHER TIPS

This is your total class.

public class MainActivity extends Activity {

    public double latitude;
    public double longitude;
    private TextView latValueLabel,lonValueLabel ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        latValueLabel = (TextView)findViewById(R.id.latLabel);
        lonValueLabel = (TextView)findViewById(R.id.lonLabel);
        Button setButton = (Button)findViewById(R.id.set_button);
        setButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {             
                latValueLabel.setText(String.valueOf(latitude));
                lonValueLabel.setText(String.valueOf(longitude));
            }
        });
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            if(location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                latValueLabel.setText(String.valueOf(latitude));
                lonValueLabel.setText(String.valueOf(longitude));
            }
        }

        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

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

        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top