未调用我的函数getNearbyRestaurant在GoogleMap上放置标记 [英] My function getNearbyRestaurant is not called to place markers on GoogleMap

查看:56
本文介绍了未调用我的函数getNearbyRestaurant在GoogleMap上放置标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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.我不确定为什么吗?我曾尝试将其放置在其他位置,但未能致电.希望任何人都能提供帮助.我想显示我当前位置附近附近餐厅的列表.我可能会错过一些东西.希望任何人都能提供帮助,因为在到达附近位置时资源不多.我看了更多有关电子叫车服务的视频

My function getNearbyRestaurant is not called. I am not sure why? I have tried to place in different locations but yet failed to call. Hope anyone can help. I am trying to show a list of nearby restaurant near my current location. I maybe missing something. Hope anyone can help as there are not much resource when it comes to getting nearby locations. I have seen more videos on e-hailing service

推荐答案

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

Both fetchLastLocation and getNearbyRestaurant load data asynchronously. If you run the code in a debugger, or add some logging, you'll see that by the time your GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude,longitude),radius) runs, the latitude = mCurrentLocation.getLatitude() lines haven't run yet.

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

Data is loaded from Firebase asynchronously and locations are retrieved from the Android OS asynchronously. The main thread of your Android app does not wait for that data or location to be available. For that reason, any code that needs the location or the data needs to be inside the relevant callback, or be called from there.

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

The simplest fix is to call getNearbyRestaurant from within 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的工作方式都是相同的.

I highly recommend reading up on asynchronous APIs, as the way this works is the same for pretty much any modern cloud or I/O based API.

请参阅:

  • getContactsFromFirebase() method return an empty list, which also shows how to make a custom callback that you can then pass into fetchLastLocation.

这篇关于未调用我的函数getNearbyRestaurant在GoogleMap上放置标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆