【问题标题】:GeoFire not getting locations [duplicate]GeoFire没有获取位置[重复]
【发布时间】:2019-04-07 12:26:09
【问题描述】:

我正在尝试使用geoFire 获取位置并将标记放置在该位置的地图上,但geoFire.getLocation() 没有运行。我尝试放置一些检查日志以查看代码出错的地方,发现在运行日志OKAY 1 和日志键后,代码直接跳转到日志OKAY 3。这是我的 logcat 的结果:

/supplier.watersystem.com.suppliersideapp 我/好的:好的 1 /supplier.watersystem.com.suppliersideapp I/KEY FOUND:station_0 /supplier.watersystem.com.suppliersideapp 我/OKAY:好的 3
11-03 19:10:25.966 32678-32678/supplier.watersystem.com.suppliersideapp E/AndroidRuntime: 致命异常: main 进程:supplier.watersystem.com.suppliersideapp,PID:32678

即使我得到的 id 也是正确的。我在放置标记的地方收到 NullPointerException,因为我的位置 ArrayList 变空了,它必须有一些值,因为如果在获取位置时出现错误,它必须记录它,否则它必须从数据库或 0,0 LatLng 中放置位置.

这是我的代码:

     //reading station data from database and adding marker
    mDatabase = FirebaseDatabase.getInstance().getReference().child("STATIONS");
    ValueEventListener stationListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int i=0;
            DatabaseReference geoFireRef = FirebaseDatabase.getInstance().getReference().child("STATION_LOCATIONS");
            GeoFire geoFire = new GeoFire(geoFireRef);
            for(DataSnapshot stationsSnapshot: dataSnapshot.getChildren()){
                stations.add(stationsSnapshot.getValue(Station.class));
                //Fetching Location
                Log.i("OKAY ", "Okay 1");
                Log.i("KEY FOUND ", stationsSnapshot.getKey());
                geoFire.getLocation(stationsSnapshot.getKey(), new LocationCallback() {
                    @Override
                    public void onLocationResult(String key, GeoLocation location) {
                        Log.i("OKAY ", "Okay 2");
                        if(location!=null){
                            Log.i("OKAY ", "Okay 2.1 not null");
                            stationLocations.add(new LatLng(location.latitude,location.longitude));
                        }
                        else{
                            Log.i("OKAY ", "Okay 2.2 null");
                            stationLocations.add(new LatLng(0,0));
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.i("Location Read [ERROR] ",databaseError.getMessage());
                    }
                });
                //placing marker
                Log.i("OKAY ", "Okay 3");
                mMap.addMarker(new MarkerOptions().position(stationLocations.get(i)).title(stations.get(i).getName()));
                Log.i("OKAY ", "Okay 4");
                i++;
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i("Station Read [ERROR]",databaseError.getMessage());
        }
    };
    mDatabase.addListenerForSingleValueEvent(stationListener);

我的数据库结构:

【问题讨论】:

  • 请检查副本以了解为什么会出现这种行为以及如何使用自定义回调解决此问题。

标签: android firebase firebase-realtime-database geolocation geofire


【解决方案1】:

我认为它不起作用,因为您的 OK2 部分是异步的,并且它没有足够的时间来获得答案,因为 OK3 部分不等待它。

【讨论】:

    猜你喜欢
    • 2019-04-11
    • 1970-01-01
    • 2014-04-25
    • 2016-09-28
    • 2014-03-09
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多