在Android的瞬间获得位置瞬间、位置、Android

2023-09-05 05:14:46 作者:君臣文武忠奸

我想点击一个按钮,接收当前的位置,我知道我不能立即得到位置,所以这是我做的: 单击事件:

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?

在requestLocationUpdates的javadoc的,有调用线程必须是环线,如呼叫活动的主线。但我没有找到任何的例子,所以我不知道这是正确的解决方案。

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.

一个问题, 请问 getLastKnownLocation()工作,即使我连接从来没有叫locationManager? 谢谢

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

推荐答案

我有同样的问题before..but我已经得到了solution..this是获得位置瞬间最简单的方法。

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));

        }
}