[英]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.