android - 在android中立即获取位置

标签 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的主线程”。但我还没有找到任何例子,所以我不知道这是否是正确的解决方案。

还有一个问题, 即使我以前从未调用过 locationManager,getLastKnownLocation() 是否也能正常工作? 谢谢

最佳答案

我以前遇到过同样的问题..但我找到了解决方案..这是即时获取位置的最简单方法。

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

        }
}

关于android - 在android中立即获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6002630/

相关文章:

android - 在 SQLite 数据库查询中设置多个选择参数

node.js - 将 GPS exif 数据写入 Node 中的图像

php - gps 位置在两个位置之间吗

android - 由于不包括大地水准面高度,Android 的 GPS 高度是否不正确?

javascript - 跟踪公交车 GPS 点是否在路线上 - Google Maps API

c++ - 适用于 Windows CE 的任何 OSTN02 库

android - 示例及说明 : Android (Studio) Login Activity Template generated activity

java - 拍照后如何在另一个 Activity 中显示图像?

安卓 : ActionBar menu item cannot be resolved or isn't a field

android - 从服务启动 Activity 并在按下主页按钮后停止之前的 Activity