从类android获取位置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从类android获取位置相关的知识,希望对你有一定的参考价值。
我正在寻找一个获得lat和long的位置的课程。我从片段调用此类,这将获得最佳提供程序并调用方法来获取位置var。我有这个变量位置,但是当我返回变量pos时,position为null;为什么?我传递这个变量?
var pos into方法没关系,但是当我返回时为null。
public class locationmng {
public Context context;
private FusedLocationProviderClient fusedLocationProviderClient;
Location position;
public locationmng(Context c){
context=c;
}
public Location GetLoc() {
if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationManager lm = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = lm.getBestProvider(criteria, true);
if (bestProvider.equals(LocationManager.NETWORK_PROVIDER)) {
position = getcoarseposition();
return position;
}
}
@SuppressLint("MissingPermission")
public Location getcoarseposition() {
final Location[] pos = new Location[1];
fusedLocationProviderClient=
LocationServices.getFusedLocationProviderClient(context);
fusedLocationProviderClient.getLastLocation()
.addOnSuccessListener((Activity) context, new
OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
pos[0] =location;
}else {
Log.i("locmng","not found loc ");
}
}
});
Log.i("locationmanager6894","ho trovato "+ a[0].getLongitude()+ "lat: "+ a[0].getLatitude());
return pos[0];
}
答案
您的位置在OnSuccessListener<Location>()
,它不会立即拨打电话。因此,您设置了一个监听器,但只有在获得您的位置时才会调用它。那么,你需要做什么:在你的听众的GetLoc()
方法中做onSuccess(Location location)
中定义的所有逻辑。
另一答案
可能并非所有位置设置都满足。您应该使用SettingsClient类提示用户更改其位置设置。文档:https://developer.android.com/training/location/change-location-settings
基本上你必须:
- 创建位置请求
protected void createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
- 获取当前位置设置
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
- 添加任务完整侦听器以了解您的位置设置是否正常
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
完成这三个步骤后,您将能够获得用户位置
以上是关于从类android获取位置的主要内容,如果未能解决你的问题,请参考以下文章
Android 使用两个不同的代码片段获取当前位置 NULL
如何在不在 android 的 Activity 上添加地图片段的情况下获取我的位置?
Android 逆向Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | Visual Studio 中 SDK 和 NDK 安装位置 )(代码片段