0

I want to be able to get the current coordinates using only GPS.

At the moment my code gets the coordinates once and then uses them over again once they have been stored. I'm looking for the solution on how to modify the code so that when the button is clicked it gets the current coordinates.

Here's the code:

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

    //Setting TextViews for the coordinates
    startLatitudeField = (TextView) findViewById(R.id.TextView02);
    startLongitudeField = (TextView) findViewById(R.id.TextView04);
    endLatitudeField = (TextView) findViewById(R.id.textView06);
    endLongitudeField = (TextView) findViewById(R.id.textView08);


    //Button
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);
            //find out how to get the current location using gps
            //not what is stored

            if (startLat != 0) {
                onLocationChanged(location);
            } else {
            onLocationChanged(location);
            Button button = (Button) findViewById(R.id.button1);
            button.setText("Get me home");
            }


        }


    }); 
};

@Override
public void onLocationChanged(Location location) {

    if (startLat == 0) {
        startLat = (double) (location.getLatitude());
        startLng = (double) (location.getLongitude());
        startLatitudeField.setText(String.valueOf(startLat));
        startLongitudeField.setText(String.valueOf(startLng));
    } 
    else {

        endLat = (double) (location.getLatitude());
        endLng = (double) (location.getLongitude());
        endLatitudeField.setText(String.valueOf(endLat));
        endLongitudeField.setText(String.valueOf(endLng));
    }

}
Bob21
  • 167
  • 10
  • If you need to get coordinates on button click, then why do you need `onLocationChanged` ? – mangusta Feb 23 '14 at 13:18
  • Not sure. I was following a guide and put that together with the little knowledge I have. If I knew the answer I would say... – Bob21 Feb 23 '14 at 13:21

2 Answers2

2

First of all you need add the permission to the Manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

You have the answer here in stackoverflow to: How do I get the current GPS location programmatically in Android?

And a sample app here: http://www.rdcworld-android.blogspot.in/2012/01/get-current-location-coordinates-city.html

Community
  • 1
  • 1
  • Thanks for this. I have got that permission in. – Bob21 Feb 23 '14 at 13:43
  • I got the basis down but then needed help getting it to work properly. Here's the post with the answer if you want to view it:http://stackoverflow.com/questions/21988087/gps-app-runs-but-shuts-down-on-button-click?answertab=active#tab-top – Bob21 Feb 26 '14 at 16:16
0

https://developer.android.com/training/location/retrieve-current.html check this they have provided sample code also

Sishin
  • 2,001
  • 1
  • 17
  • 22