未调用我的函数getNearbyRestaurant在GoogleMap上放置标记

问题描述 投票:0回答:1
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.google_map);


    restaurantNearbyRef = FirebaseDatabase.getInstance().getReference().child("Restaurant").child("Info");
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);


    fetchLastLocation();
    getNearbyRestaurant();


}


private ArrayList<Marker> restaurantMarker = new ArrayList<>();

private void fetchLastLocation() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
        ActivityCompat.requestPermissions(this, new String[]
                {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
        return;
    }
    Task<Location> task = fusedLocationProviderClient.getLastLocation();
    task.addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if (location != null) {

                mCurrentLocation = location;
                Toast.makeText(getApplicationContext(), mCurrentLocation.getLatitude()
                        + "" + mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
                SupportMapFragment supportMapFragment = (SupportMapFragment)
                        getSupportFragmentManager().findFragmentById(R.id.google_map);
                supportMapFragment.getMapAsync(MapActivity.this);

                latitude = mCurrentLocation.getLatitude();
                longitude = mCurrentLocation.getLongitude();

                Log.d(TAG, "The latitude is " + latitude + " and the longitude is " + longitude);
            }

        }

    });

}


public void getNearbyRestaurant(){
    if (restaurantMarker != null) {
        for (Marker marker : restaurantMarker) {
            marker.remove();
        }
    }

    GeoFire geoFire = new GeoFire(restaurantNearbyRef);

    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude,longitude),radius);
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            restaurantMarker
                    .add(mMap.addMarker(new MarkerOptions().title("Restaurant").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                            .position(new LatLng(location.latitude, location.longitude))));

            Log.d(TAG,"The getNearbyRestaurant latitude is " + latitude + "and the longitude is " +longitude );
        }

        @Override
        public void onKeyExited(String key) {

        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {

        }

        @Override
        public void onGeoQueryReady() {

        }

        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    });
}

未调用我的函数getNearbyRestaurant。我不确定为什么吗?我尝试将其放置在其他位置,但未能打电话。希望任何人都能提供帮助。我想显示我当前位置附近附近餐厅的列表。我可能会错过一些东西。希望任何人都能提供帮助,因为到附近位置时资源不多。我看过更多有关电子叫车服务的视频

android firebase firebase-realtime-database geofire
1个回答
0
投票

fetchLastLocationgetNearbyRestaurant都异步加载数据。如果您在调试器中运行代码,或添加一些日志记录,则将看到GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude,longitude),radius)运行时,latitude = mCurrentLocation.getLatitude()行尚未运行。

数据是从Firebase异步加载的,位置是从Android OS异步检索的。 Android应用程序的主线程不会等待该数据或位置可用。因此,任何需要位置或数据的代码都必须位于相关的回调中,或从那里进行调用。

最简单的解决方法是从getNearbyRestaurant中调用onSuccess

Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
    @Override
    public void onSuccess(Location location) {
        if (location != null) {

            mCurrentLocation = location;
            Toast.makeText(getApplicationContext(), mCurrentLocation.getLatitude()
                    + "" + mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
            SupportMapFragment supportMapFragment = (SupportMapFragment)
                    getSupportFragmentManager().findFragmentById(R.id.google_map);
            supportMapFragment.getMapAsync(MapActivity.this);

            latitude = mCurrentLocation.getLatitude();
            longitude = mCurrentLocation.getLongitude();

            Log.d(TAG, "The latitude is " + latitude + " and the longitude is " + longitude);

            getNearbyRestaurant();
        }

我强烈建议您阅读异步API,因为几乎所有基于现代云或I / O的API的工作方式都是相同的。

参见:

© www.soinside.com 2019 - 2024. All rights reserved.