通过为 Android 在 Google 地图中更新 GeoFire 位置来移动多个标记图标

Multiple Marker Icon Move by Updating GeoFire Location in Google Maps for Android

我正在尝试使用 geo-fire 在 1 公里半径内显示所有骑手在 google 地图中的位置。但在 google 地图中只显示一个骑手标记图标。虽然我得到了所有车手的纬度和经度。

截图:

https://imgur.com/p0wUxRB.jpg

代码:

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        updateRiderPosition(new GeoLocation(location.getLatitude(), location.getLongitude()));
    }
}

public void updateRiderPosition(GeoLocation location) {
    ArrayList<Marker> markerList = new ArrayList<>();

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference(mPositionNode).child(mAuthId);
    GeoFire geoFire = new GeoFire(ref);
    GeoQuery geoQuery = geoFire.queryAtLocation(location, 1);
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            //---------------------------------------------------
            if (markerList != null) {
                for (Marker marker : markerList) {
                    marker.remove();
                }
            }
            markerList.add(mMap.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude)).icon(icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_motorbike)))));
            //---------------------------------------------------
        }

        @Override
        public void onKeyExited(String key) {}

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

        @Override
        public void onGeoQueryReady() {}

        @Override
        public void onGeoQueryError(DatabaseError error) {}
    });

}

在添加新标记之前在 onKeyEntered 中删除所有以前的标记

@Override
    public void onKeyEntered(String key, GeoLocation location) {
        //---------------------------------------------------
        // this if remove all previous markers
        if (markerList != null) {
            for (Marker marker : markerList) {
                marker.remove();
            }
        }
        markerList.add(mMap.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude)).icon(icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_motorbike)))));
        //---------------------------------------------------
    }
@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        updateRiderPosition(new GeoLocation(location.getLatitude(), location.getLongitude()));
    }
}

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

private void updateRiderPosition(GeoLocation location) {
    if (markerList != null) {
        for (Marker marker : markerList) {
            marker.remove();
        }
    }

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference(mPositionNode).child(mAuthId);
    GeoFire geoFire = new GeoFire(ref);
    GeoQuery geoQuery = geoFire.queryAtLocation(location, 1);
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            markerList.add(mMap.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude)).icon(icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_motorbike)))));
        }

        @Override
        public void onKeyExited(String key) {}

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

        @Override
        public void onGeoQueryReady() {}

        @Override
        public void onGeoQueryError(DatabaseError error) {}
    });

}