在上面的树中,我已经存储了driversAvailable的当前纬度和经度。如果driversAvailable树下有多个驱动程序,那么如何在地图上显示所有这些驱动程序呢?到目前为止,我已经编写了以下代码,但它无效:
private DatabaseReference driverLoadLocationRef;
private ValueEventListener driverLoadLocationRefListener;
private void loadAllAvailableDrivers() {
DatabaseReference driverLoading = FirebaseDatabase.getInstance().getReference().child("driversAvailable");
GeoFire geoDriver = new GeoFire(driverLoading);
GeoQuery geoQueryDriver = geoDriver.queryAtLocation(new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()),distance);
geoQueryDriver.removeAllListeners();
geoQueryDriver.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(final String key, GeoLocation location) {
if (!driverLoad ) {
driverLoad = true;
driverLoadId = key;
driverLoadLocationRef = FirebaseDatabase.getInstance().getReference().child("driversAvailable").child(driverFoundId).child("l");
driverLoadLocationRefListener = driverLoadLocationRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists() ) //If doesnot exist check then app will crash
{
List<object> map = (List<object>) dataSnapshot.getValue();
double locationLoadLat = 0;
double locationLoadLng = 0;
if (map.get(0) != null)
{
locationLoadLat = Double.parseDouble(map.get(0).toString());
}
if (map.get(1) != null)
{
locationLoadLng = Double.parseDouble(map.get(1).toString());
}
LatLng driverLoadLatLng = new LatLng(locationLoadLat,locationLoadLng);
if (mDriverMarker != null) //If not added app crash since it will try to remove which doesnot exist
{
mDriverMarker.remove();
}
mDriverMarker = mMap.addMarker(new MarkerOptions().position(driverLoadLatLng).flat(true)
.title(driverLoadId)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car3)));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(PassengerMapsActivity.this,"Failed",Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
if (distance <= LIMIT)
{
distance++;
loadAllAvailableDrivers();
}
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
答案 0 :(得分:0)
我认为我有更简单的方法来实现它!
创建Marker数组,使用onDataChange的条目数(childrenCount)初始化数组。 之后,在for(DataSnapshot ds:dataSnapshot.getChildren())'中嵌入另一个for..loop,迭代找到的条目数(childrenCount)。
我假设您有一个用于将数据推送到firebase的Model类。用它来检索特定的坐标。 Av决定将'0'和'1'替换为'lat'和'lon'。
private void loadAllAvailableDrivers() {
DatabaseReference driverLoading = FirebaseDatabase.getInstance().getReference("driversAvailable");
driverLoading.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//trying to get several markers
//Create an array and specify a size which will be initialized to children(driver) count
int size = (int) dataSnapshot.getChildrenCount(); //
Marker[] allMarkers = new Marker[size];
map.clear();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
//Specify your model class here
ModelClass modelObject=new ModelClass();
//lets create a loop
for(int i=0;i<=size;i++) {
try {
//am assuming the model class uses variable 'lat',lon,name
modelObject.setLat(ds.getValue(ModelClass.class).getLat());
modelObject.setLon(ds.getValue(ModelClass.class).getLon());
modelObject.setName(ds.getValue(ModelClass.class).getName());
//lets retrieve the coordinates and store them in a variable
String myName=modelObject.getName();
Double latitude = Double.parseDouble(modelObject.getLat());
Double longitude = Double.parseDouble(modelObject.getLon());
//convert the coordinates to LatLng
LatLng latLng = new LatLng(latitude, longitude);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Now lets add updated markers
//lets add updated marker
allMarkers[i] = map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car3)).position(latLng).title(myName));
}catch (Exception ex){}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(PassengerMapsActivity.this,"Failed",Toast.LENGTH_SHORT).show();
}
});
}