Deepak Sharma
Deepak Sharma

Reputation: 5073

how to get lat and long in android

I am facing a minor problem in my code and getting stuck due to it.Following is my code:-

public class MainActivity extends Activity {
TextView textView1;
Location currentLocation;
double currentLatitude,currentLongitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView1 = (TextView) findViewById(R.id.textView1);

    findLocation();

    textView1.setText(String.valueOf(currentLatitude) + "\n"
            + String.valueOf(currentLongitude));

}

 public void findLocation() {

        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {

            public void onLocationChanged(Location location) {

                updateLocation(location,currentLatitude,currentLongitude);

                Toast.makeText(
                        MainActivity.this,
                        String.valueOf(currentLatitude) + "\n"
                                + String.valueOf(currentLongitude), 5000)
                        .show();

                }

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

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        };

        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

    }


    void updateLocation(Location location,double currentLatitude,double currentLongitude) {
            currentLocation = location;
            this.currentLatitude = currentLocation.getLatitude();
            this.currentLongitude = currentLocation.getLongitude();

        }
}

Every thing is working fine.But my problem is that the class level variables currentLatitude and currentLongitude having the values null.In the above code when i am setting the lat and long in text view in update location method,it works fine but when i want to set the same values in text view in on create method it gives null value.Why i dont know.Please help to sort out this problem.Thanks in advance!

Upvotes: 0

Views: 220

Answers (3)

Ken Wolf
Ken Wolf

Reputation: 23279

Finding the location takes some time before it returns with values. It happens asynchronously and meanwhile your UI thread continues.

So in onCreate(), you have no location yet. It doesn't matter the order of the calls in onCreate().

You only get location when your updateLocation() method is called, and there is no guarantee of when that happens in relation to your views being set up.

So, to fix, update your textView texts in there.

void updateLocation(Location location,double currentLatitude,double currentLongitude) {
    currentLocation = location;
    this.currentLatitude = currentLocation.getLatitude();
    this.currentLongitude = currentLocation.getLongitude();
    textView1.setText(String.valueOf(currentLatitude) + "\n"
        + String.valueOf(currentLongitude));
}

Upvotes: 0

g00dy
g00dy

Reputation: 6788

I would suggest you to put textView1.setText(String.valueOf(currentLatitude) + "\n" + String.valueOf(currentLongitude)); in the updateLocation function.

Upvotes: 0

Armaan Stranger
Armaan Stranger

Reputation: 3140

Its because When you set text in textview in Oncreate method lat and long is not initialized. it will get initialized when it gets updated.

so you should settext in updatelocation() method.

locationlistener take sometime to update its lat and long so that your oncreate method is executed meanwhile so that your lat and long is not updated and stays null. so its better to settext on updatelocation.

Hope it Helps!!

Upvotes: 1

Related Questions