1

I want to click a button and receive the current location, i understand that i can't instantly get the location , so this is what i did : the click event :

        public void onClick(View v)
        {
            ProgressDialog MyDialog = ProgressDialog.show( MainPage.this, " " , " Loading. Please wait ... ", true);
            MyActionsHandler myActionsHandler = new myActionsHandler(MainPage.this);
            myActionsHandler.startSearch();
            MyDialog.dismiss();
            Intent intent = new Intent(MainPage.this, ResultPage.class);
            startActivity(intent);
        }

and this is the handler that searches for the location

    public void startSearch(long timeInterval,float distanceInterval)
{
    LocationManager lm = (LocationManager)_context.getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeInterval,
            distanceInterval, this);

    while(!_locationFound)
    {
        //wait till location is found
    }
}

public void onLocationChanged(Location location)
{
    if (location != null)
    {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        float speed = location.getSpeed();
        float bearing = location.getBearing();

        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        try
        {
            doTheProcess(_searchType,latitude, longitude, speed, bearing);
           _locationFound = true;
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I understand that this doesn't work, because the loop is in the same thread, so what do you suggest the best solution to do it?

in the javadoc of requestLocationUpdates , there is "The calling thread must be a Looper thread such as the main thread of the calling Activity." but i haven't found any example so i don't know if it's the right solution.

one more question, does the getLastKnownLocation() work even i fi never called the locationManager before? thanks

Dany Y
  • 6,833
  • 6
  • 46
  • 83

2 Answers2

4

I have same problem before..but I have got the solution..this is the simplest way to get location instantly.

public class LocationFinder extends Activity {

    TextView textView1;
    Location currentLocation;
    double currentLatitude,currentLongitude;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textView1 = (TextView) findViewById(R.id.textView1);
        Log.i("@@@@@@@@@@ Inside LocationFinder onCreate", "LocationFinder onCreate");

        FindLocation();

    }

    public void FindLocation() {
        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                updateLocation(location);

                Toast.makeText(
                        LocationFinder.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) {
            currentLocation = location;
            currentLatitude = currentLocation.getLatitude();
            currentLongitude = currentLocation.getLongitude();
            textView1.setText(String.valueOf(currentLatitude) + "\n"
                    + String.valueOf(currentLongitude));

        }
}
Chirag_CID
  • 2,224
  • 1
  • 24
  • 33
0

You could get rid of the _locationFound variable entirely - what were you originally going to have in the

while(!_locationFound) {}

block?

If you get rid of that, and move anything you would originally had in that block into your doTheProcess function (or where you set _locationFound to true) then it should work I believe.

actionshrimp
  • 5,219
  • 3
  • 23
  • 26
  • hi i just updated my post, i'm using `while(!_locationFound) {}` to always run the progress dialog until it finds the location. – Dany Y May 14 '11 at 15:31
  • 1
    In that case you could make ProgressDialog myDialog a private member variable of your class. Create the dialog in the onClick() function as you have done, and then forget about it. You can then call myDialog.dismiss() in the onLocationChanged function (although you will probably want to check if it has already been dismissed first, as onLocationChanged will keep getting called until you unsubscribe from location updates. – actionshrimp May 14 '11 at 15:35
  • you think this is the best way? i prefered to make the action of getting the location independant from the caller. so that i could call it from other places (service,other activity...) or maybe i've got the concept wrong? – Dany Y May 14 '11 at 19:57