实践是知识的母亲,知识是生活的明灯。这篇文章主要讲述从类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")
publicLocation 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
【从类android获取位置】基本上你必须:
- 创建位置请求
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 Studio)
- android检查位置服务启用播放服务位置api
- java.lang.IllegalStateException(无法执行android的方法:onClick访问变量Android [重复])
- 映射Android开发的跟踪要求
- 如何在Polymer中使用app-localstorage-document
- app-header内的聚合物app-drawer无法正常工作
- 如何在聚合物2中使用app-route来获得深层路径()
- 以编程方式调整Android视图大小