【问题标题】:Getting location takes a long time in Android App在Android App中获取位置需要很长时间
【发布时间】:2018-11-12 20:19:52
【问题描述】:

我正在开发一个 Android 应用程序,用于访问用户的当前位置以及其他内容。但同时纬度和经度返回 0.0 和 0.0 ,然后过了一会儿,它显示了一些值。 我也使用过 FusedLocationAPI(currently deprecated) 和 FusedLocationProviderClient API,但也得到了相同的结果。我使用了 LocationManager 但根本没有改进。我在下面附上了当前代码。 谁能告诉我一个更好的方法来访问位置信息?在此先感谢:)

private void Locate() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {

        String[] permits = {
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION};

        if (!PermissionsAs(this, permits)) {
            ActivityCompat.requestPermissions(this, permits, REQ_CODE);
        }
        return;
    } else if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            double lattitude = location.getLatitude();
            double longitude = location.getLongitude();

            String string = getCompleteAddressString(lattitude, longitude);

            System.out.println(string);

            System.out.println("Longitude: " + String.valueOf(lattitude) + " Lattitude: " + String.valueOf(lattitude) + "\n");
            Toast.makeText(getApplicationContext(), "" + longitude + " " + lattitude, Toast.LENGTH_LONG).show();
            sendText(lattitude, longitude);
        }
    }
}

【问题讨论】:

  • getLastKnownLocation() 不请求位置更新。它返回另一个应用程序的最后(最近)请求。如果最近没有其他应用程序请求该位置,它将返回 0。您也只使用网络提供商。使用LocationManager.NETWORK_PROVIDER | LocationManager.GPS_PROVIDER
  • 谢谢。但你能更具体一点吗? @TheWanderer

标签: android android-location


【解决方案1】:

您可以使用这个库,它会自动处理位置提供程序并为您提供最佳位置,我也在我的应用程序中使用它并且效果很好。 Location Provider Library

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2014-01-14
    • 2016-10-21
    相关资源
    最近更新 更多