简体   繁体   English

在Android App中获取位置需要很长时间

[英]Getting location takes a long time in Android App

I was developing an android app for a while which used to access the current location of a user and then other things. 我开发了一段时间的android应用程序,用于访问用户的当前位置,然后访问其他内容。 But meanwhile the latitude and longitude return 0.0 & 0.0 and then after a while, it shows some values. 但是同时纬度和经度返回0.0和0.0,然后过一会儿,它显示了一些值。 I have had used FusedLocationAPI(currently deprecated) and FusedLocationProviderClient API too but got the same result as well. 我也使用过FusedLocationAPI(当前已弃用)和FusedLocationProviderClient API,但也得到了相同的结果。 I used LocationManager but no improvements at all. 我使用了LocationManager,但完全没有任何改进。 I attached the current code down below. 我在下面附加了当前代码。 Could anyone show me a better way to access the location information? 谁能告诉我一种访问位置信息的更好方法? Thanks in advance :) 提前致谢 :)

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

you could use this library, it handles the location providers automatically and give you the best possible location, I'm using it in my application as well and it works well. 您可以使用此库,它会自动处理位置提供程序并为您提供最佳位置,我也在应用程序中使用它,并且效果很好。 Location Provider Library 位置提供者库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Location Manager需要很长时间才能找到经度和纬度 - Android Location Manager Takes A Long Time Finding Longitude and Latitude Android Studio在真实设备上运行应用程序需要太长时间 - Android Studio Takes too long time to run the app on real device Android APP花费太长时间才能首次在设备上打开 - Android APP takes too long to open on device for first time 在Google Map FragmentActivity中获取用户位置需要很长时间 - Getting user location takes a very long time inside Google Map FragmentActivity 获取经度和纬度需要很长时间 - Getting longitude and latitude takes a very long time Android:在Mapview中进行位置更新需要很长时间,甚至很难找到位置 - Android : Location Update in Mapview takes to long even tough Location is found 我的 Android 应用程序在我第一次打开时需要很长时间才能加载 - My Android app takes a long time to load the first time I open it 在Android中获取联系方式花费的时间太长 - Getting contact details takes too long in Android Android意向需要很长时间才能收到 - Android intent takes a long time to be received Android:eclipse工作区需要很长时间才能构建? - Android: eclipse workspace takes a long time to build?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM