java - 使用当前位置时,我不断获取位置空值
问题描述
我正在尝试使用 Geoquery 在我的半径范围内查找其他最近的用户。在我的应用程序中,我有一个按钮,按下该按钮会调用一个名为 findClosestCars 的函数,该函数使用 geoQuery 到我半径内最近的用户。为了使用 Geoquery,我需要获取我的纬度和经度。为此,我将纬度和经度存储在名为纬度和经度的双精度类型变量中。纬度和经度被名为 currentlocation 的位置对象获取,例如 latitude=currentLocation.getLatitude(),longitude=currentLocation.getLongitude()。我没有使用 fusedlocation,而是尝试获取当前位置来填充对象 currentLocation。下面是我的 findClosestCars 代码
private void findClosestCars() {
latitude=currentLocation.getLatitude();
longitude=currentLocation.getLongitude();
databaseReference = FirebaseDatabase.getInstance().getReference("Lenders");
GeoFire geoFire = new GeoFire(databaseReference);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude, longitude), radius);
geoQuery.removeAllListeners();
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
if (!driverFound) {
driverFound = true;
driverFoundID = key;
closestLender = FirebaseDatabase.getInstance().getReference("Users").child("Lenders").child(driverFoundID);
;
showPictureAndPrice();
}
radius++;
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
if (!driverFound) {
radius++;
}
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
下面是获取当前位置并将其放入变量 currentLocation 的代码
@Override
public void onLocationChanged(@NonNull Location location) {
currentLocation=location;
}
protected synchronized void builGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
}
解决方案
您正在检查权限,但如果您还没有权限,则不会请求它们。你需要这样做。
推荐阅读
- typescript - TypeScript + EsLint 缩进:预期缩进 4 个空格,但发现 6
- javascript - div后无法添加按钮
- r - 计算使用逻辑回归创建的模型的准确性
- elasticsearch - 根据单词索引更改搜索顺序
- uml - 如何在 UML 中的关联 memberEnd 处获取实体的属性?
- vuejs2 - Vue.js:如何跨 3 列进行路由
- c# - 用户登录 asp.net core 3 后 Cookie 身份验证未重定向
- python - AUTO ARIMA MODEL 在 python 中预测每小时数据
- java - 指针是否在本地声明的末尾关闭?
- java - 在Tomcat中将Spring引导部署为战争时未加载CSS JS和链接