在android中立即获取位置

时间:2011-05-14 15:01:07

标签: android gps

我想点击一个按钮并接收当前位置,我知道我无法立即获取位置,所以这就是我所做的: 点击事件:

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

这是搜索位置的处理程序

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

我知道这不起作用,因为循环在同一个线程中, 那么你建议做什么最好的解决方案呢?

在requestLocationUpdates的javadoc中,有“调用线程必须是Looper线程,例如调用Activity的主线程”。但我没有找到任何例子,所以我不知道这是否是正确的解决方案。

还有一个问题, getLastKnownLocation()是否工作,即使我之前从未调用过locationManager? 感谢

3 个答案:

答案 0 :(得分:3)

我之前遇到同样的问题..但我有解决方案..这是立即获取位置的最简单方法。

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

        }
}

答案 1 :(得分:0)

你可以完全摆脱_locationFound变量 - 你最初将在

中拥有什么
while(!_locationFound) {}

阻止?

如果你摆脱它,并将你原来在该块中的任何东西移动到你的doTheProcess函数中(或者你将_locationFound设置为true)那么它应该可以工作我相信。

答案 2 :(得分:0)

我遇到了类似的地方,我无法收到位置更新,将LocationListener与我自己的Threads / HandlerThreads混合在一起。解决方案是使用PendingIntent和requestLocationUpdates(provider,minTime,minDistance,intent)。看看:How to receive location updates without using a service