我在firebase数据库中有一个节点(可用的驱动程序),其中每个驱动程序都有一个地理位置节点和一个跟踪节点。我想根据“ track”的值在地图上指定标记的颜色
我尝试将侦听器添加到driversAvailableDatabaseref
@Override
public void onKeyEntered(final String key, final GeoLocation location) {
driversAvailableDatabaseRef.child(key).child(getString(R.string.track)).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
String track = "default";
track = dataSnapshot.getValue(String.class);
Marker marker;
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(location.latitude, location.longitude));
if (track.equals(getString(R.string.green))) {
// Bitmap greenBusIcon = BitmapFactory.decodeResource(getResources(), R.drawable.green_bus);
marker = mMap.addMarker(markerOptions.icon(bitmapDescriptorFromVector(StudentMapsActivity.this, R.drawable.green_bus)));
} else if (track.equals(getString(R.string.red))) {
// Bitmap redBusIcon = BitmapFactory.decodeResource(getResources(), R.drawable.red_bus);
marker = mMap.addMarker(markerOptions.icon(bitmapDescriptorFromVector(StudentMapsActivity.this, R.drawable.red_bus)));
} else {
marker = mMap.addMarker(markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}
markers.put(key, marker);
} catch (NullPointerException e) {
Toast.makeText(StudentMapsActivity.this, "No drivers available", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
即使当前已登录驱动程序,此代码也仅显示Toast“没有可用的驱动程序”。
我尝试了这段代码
@Override
public void onKeyEntered(final String key, final GeoLocation location) {
Marker marker;
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(location.latitude, location.longitude));
marker = mMap.addMarker(markerOptions.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
markers.put(key, marker);
这可以正常工作,但不会显示不同的颜色,具体取决于曲目
(您可能会注意到轨道也在实时变化,如果您也可以帮忙的话,我也不想这样)