如何在Android上获取当前GPS位置而无需等待位置变化?

4

我希望通过gps供应商获取设备的当前位置,但实际上需要等待gps位置发生变化。

因此,我有一个简单的应用程序来监听位置变化。这可以立即使用NETWORK_PROVIDER功能,但对于GPS_PROVIDER,我等了15分钟都没有反应。

我正在使用API级别8。

public class MainActivity extends Activity {
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) this.findViewById(R.id.textView1);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();

        //lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); // this works fine
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }

    private class mylocationlistener implements LocationListener {
        public void onLocationChanged(Location location) {
            Date today = new Date();
            Timestamp currentTimeStamp = new Timestamp(today.getTime());
            if (location != null) {
                Log.d("LOCATION CHANGED", location.getLatitude() + "");
                Log.d("LOCATION CHANGED", location.getLongitude() + "");
                String str = "\n CurrentLocation: " + "\n Latitude: "
                        + location.getLatitude() + "\n Longitude: "
                        + location.getLongitude() + "\n Accuracy: "
                        + location.getAccuracy() + "\n CurrentTimeStamp "
                        + currentTimeStamp + "\n Altitude: "
                        + location.getAltitude() + "\n Bearing"
                        + location.getBearing() + "\n Speed"
                        + location.getSpeed() + "\n ";
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG)
                        .show();
                tv.append(str);
            }
        }

        public void onProviderDisabled(String provider) {
            Toast.makeText(MainActivity.this, "Error onProviderDisabled",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String provider) {
            Toast.makeText(MainActivity.this, "onProviderEnabled",
                    Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(MainActivity.this, "onStatusChanged",
                    Toast.LENGTH_LONG).show();
        }
    }
}

我在清单文件中添加了权限,因此这不是问题所在。


你确定你已经启用了GPS吗? - gsgx
这取决于您的设备和GPS信号强度,因为新型手机具有高灵敏度的GPS系统,但旧手机需要开阔的天空来检测信号。 - Ali Imran
2个回答

2

你是在室内测试吗?除非你在户外,否则可能根本无法获得GPS锁定,这意味着手机会尽可能地尝试获取锁定(因为你在requestLocationUpdates调用中指定了最小时间和距离为0和0)。

不幸的是,你只能等待设备获取GPS锁定,如果由于信号不足而无法获取,则只能继续等待。你可以做的第一件事是尝试获取GPS提供程序的上次已知位置。如果设备最近已经获取了GPS锁定并存储了位置,则会返回位置:

Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

除此之外,您应该尝试从网络提供商获取位置信息,因为这很可能比GPS更快地返回位置信息,正如您已经观察到的那样。这是因为位置数据来源(基站和WIFI)非常丰富,在室内外都可以获得,只要您有无线信号。


是的,我在室内,我没有意识到在室内会让我失去所有信号...我想我只能使用模拟器了... - Alexandru Chirila
1
这是个好主意。还有一个非常棒的应用程序叫做“Fake GPS”,可以让你模拟位置锁定以进行测试。如果你只需要调试基于位置的应用程序,我强烈建议你尝试一下。这是链接:https://play.google.com/store/apps/details?id=com.lexa.fakegps - mattgmg1990

0
try{
                LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


                //Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                double longitude = location.getLongitude();
                double latitude = location.getLatitude();
                String stlongitude = Double.toString(longitude);
                String stlatitude = Double.toString(latitude);

                TextView celllocation = (TextView) findViewById(R.id.txtCellLocation);
                celllocation.setText(stlatitude + " , " + stlongitude);


                //String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
                //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                //startActivity(intent);
    }

    catch (Exception a) {
        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        String stlongitude = Double.toString(longitude);
        String stlatitude = Double.toString(latitude);

        TextView celllocation = (TextView) findViewById(R.id.txtCellLocation);
        celllocation.setText(stlatitude + " , " + stlongitude);

    } 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接