首页 > 解决方案 > 使用当前位置时,我不断获取位置空值

问题描述

我正在尝试使用 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);
    }


标签: javaandroidfirebase-realtime-databaselocationgeofire

解决方案


您正在检查权限,但如果您还没有权限,则不会请求它们。你需要这样做。


推荐阅读