我正在使用geofire库从特定区域的firebase3加载数据,但是当我在数据库中添加新的标记并检查我的地图时,我发现在相同的延迟坐标中添加了多个标记,我的问题是如何防止标记在相同的坐标中重复。以下是Geofire方法:
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
try{
items.add(new MyItem(location.latitude, location.longitude));
Log.d("onKey","called");
}catch (ClassCastException e){
Log.d("classCastException","");
}
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
parseJsonToList();
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
ParseJsonToList方法:
private void parseJsonToList() {
itemss = clusterManagerAlgorithm.getItems();
try {
items.removeAll(itemss);
}catch (IndexOutOfBoundsException e){
Log.d("itemsDoesn't exist"," : ");
}
mClusterManager.clearItems();
mClusterManager.cluster();
mClusterManager.addItems(items);
Log.d("items"," : " + items);
}
发布于 2018-09-16 10:09:33
根据您发布的代码,有两点可能是问题的根源。
首先,它是GeoQueryEventListener.onKeyEntered()
的多个调用,要修复它,您可以采用如下解决方法:
try{
MyItem myItem = new MyItem(location.latitude, location.longitude);
if (!items.contains(myItem)) {
items.add(myItem);
}
Log.d("onKey","called");
}catch (ClassCastException e){
Log.d("classCastException","");
}
其次,您可能忘记了覆盖MyItem
类的equals()
和hashcode()
方法。因此,当您调用items.removeAll(itemss);
时,没有任何项被删除。
在这两种情况下,都需要重写MyItem
类的equals()
和hashcode()
方法。在AndroidStudio中使用the autogenerating function很容易。
https://stackoverflow.com/questions/52349775
复制