【问题标题】:How to get current GPS position without waiting for location change on Android?如何在不等待 Android 上的位置更改的情况下获取当前 GPS 位置?
【发布时间】:2012-12-10 15:58:59
【问题描述】:

我想使用 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 已启用吗?
  • 这取决于您的设备以及 GPS 信号强度,因为较新的手机具有高灵敏度 GPS 系统,但旧手机需要开阔的天空来检测信号。

标签: android gps location


【解决方案1】:

你在里面测试吗?除非您在外面,否则您可能根本无法获得 GPS 锁定,这意味着手机将尽可能多地尝试获得锁定(因为您在 requestLocationUpdates 调用中指定了最短时间和距离为 0 和 0 )。

不幸的是,您只需要等待设备获得 GPS 锁定,如果不是因为信号不足,您将继续等待。缓解这种情况的第一件事是尝试获取 GPS 提供商的最后一个已知位置。如果设备最近有 GPS 锁定并存储了一个位置,这将返回一个位置:

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

除此之外,您应该尝试从网络提供商那里获取位置,因为正如您已经观察到的那样,这可能会比 GPS 更快地返回位置。这是因为有大量的位置数据来源(手机信号塔和 WIFI),只要您有无线信号,就可以在任何地方(包括室内)使用。

【讨论】:

  • 是的,我在里面,我没有意识到在里面会让我失去所有的信号......我猜我只需要使用模拟器......
  • 这是个好主意。还有一个其他人制作的名为“Fake GPS”的出色应用程序,可让您模拟位置锁定以进行测试。如果您只需要调试基于位置的应用程序,我强烈建议您尝试一下。这是链接:play.google.com/store/apps/details?id=com.lexa.fakegps
【解决方案2】:
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);

    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多