Итак, наконец, я получил решение для этого, так как @Andy сказал, что я сохранил коллекцию для маркеров и изменил код следующим образом:
1.Создайте список маркеров
List<Marker> markerList = new CopyOnWriteArrayList<>();
2.Получить список всех маркеров из базы данных
public void initialListCreation() {
new Thread(new Runnable() {
int width = 50;
int height = 50;
BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.car);
Bitmap b = bitmapDrawable.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
@Override
public void run() {
final Gson gson = new Gson();
final StringBuilder stringBuilder = new StringBuilder();
final PaginatedList<VehicleLocationsDO> onTrackVehicles = dynamoDBMapper.scan(VehicleLocationsDO.class, new DynamoDBScanExpression());
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < onTrackVehicles.size(); i++) {
String OnTrackVehiclesString = gson.toJson(onTrackVehicles.get(i));
stringBuilder.append(OnTrackVehiclesString + "\n");
JsonModel onTrackVehiclesObject = gson.fromJson(OnTrackVehiclesString, JsonModel.class);
int fetchedVehicleId = onTrackVehiclesObject.get_vehicleId();
double fetchedLatitude = onTrackVehiclesObject.get_latitude();
double fetchedLongitude = onTrackVehiclesObject.get_longitude();
LatLng latLng = new LatLng(fetchedLatitude, fetchedLongitude);
marker = mMap.addMarker(new MarkerOptions()
.anchor(.5f,.5f)
.position(latLng)
.title("Vehicle No. "
+String.valueOf(fetchedVehicleId))
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
marker.setTag(fetchedVehicleId);
markerList.add(marker);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}).start();
}
3.Затем создайте метод для отображения маркеров и анимируйте их следующим образом:
public void showMarker() {
new Thread(new Runnable() {
int width = 50;
int height = 50;
BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.car);
Bitmap b = bitmapDrawable.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
@Override
public void run() {
final Gson gson = new Gson();
final StringBuilder stringBuilder = new StringBuilder();
final PaginatedList<VehicleLocationsDO> onTrackVehicles = dynamoDBMapper.scan(VehicleLocationsDO.class, new DynamoDBScanExpression());
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
if (onTrackVehicles.isEmpty())
Toast.makeText(getApplicationContext(), "No Vehicle on Track at present", Toast.LENGTH_SHORT).show();
else {
for (int i = 0; i < onTrackVehicles.size(); i++) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : markerList) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
int padding = 70; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
if (markerList.isEmpty()) {
String OnTrackVehiclesString = gson.toJson(onTrackVehicles.get(i));
stringBuilder.append(OnTrackVehiclesString + "\n");
JsonModel onTrackVehiclesObject = gson.fromJson(OnTrackVehiclesString, JsonModel.class);
int fetchedVehicleId = onTrackVehiclesObject.get_vehicleId();
double fetchedLatitude = onTrackVehiclesObject.get_latitude();
double fetchedLongitude = onTrackVehiclesObject.get_longitude();
LatLng latLng = new LatLng(fetchedLatitude, fetchedLongitude);
marker = mMap.addMarker(new MarkerOptions()
.anchor(.5f,.5f)
.position(latLng)
.title("Vehicle No. "
+ String.valueOf(fetchedVehicleId))
.icon(BitmapDescriptorFactory
.fromBitmap(smallMarker)));
marker.setTag(fetchedVehicleId);
markerList.add(marker);
} else {
for (final Marker m : markerList) {
String OnTrackVehiclesString = gson.toJson(onTrackVehicles.get(i));
stringBuilder.append(OnTrackVehiclesString + "\n");
JsonModel onTrackVehiclesObject = gson.fromJson(OnTrackVehiclesString, JsonModel.class);
final int fetchedVehicleId = onTrackVehiclesObject.get_vehicleId();
double fetchedLatitude = onTrackVehiclesObject.get_latitude();
double fetchedLongitude = onTrackVehiclesObject.get_longitude();
LatLng latLng2 = new LatLng(fetchedLatitude, fetchedLongitude);
int markerTag = (int) m.getTag();
if (m.getTag() != null) {
if (markerTag == fetchedVehicleId) {
new Thread(new Runnable() {
@Override
public void run() {
final VehicleLocationsDO readNewLocation = dynamoDBMapper.load(VehicleLocationsDO.class, fetchedVehicleId);
final LatLng updatedLatLng = new LatLng(readNewLocation.getLatitude(), readNewLocation.getLongitude());
runOnUiThread(new Runnable() {
@Override
public void run() {
double bearing = bearingBetweenLocations(m.getPosition(), updatedLatLng);
rotateMarker(m, (float) bearing);
MarkerAnimation.animateMarkerToGB(m, updatedLatLng, new LatLngInterpolator.Spherical());
}
});
}
}).start();
}
} else {
marker = mMap.addMarker(new MarkerOptions()
.position(latLng2)
.title(String.valueOf(fetchedVehicleId))
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
marker.setTag(fetchedVehicleId);
markerList.add(marker);
}
}
}
}
}
} catch (Exception e) {
Log.e("Exception caught", e.toString() + " " + e.getMessage() + " " + e.getCause());
}
}
});
}
}).start();
}
4.Последний шаг Теперь вам просто нужно вызвать метод initialListCreation () только один раз в методе onCreate (), и вы можете вызывать showMarker () с периодическим интервалом 2000-3000 (мс), используя TimerTask.
Это все.Наслаждайтесь !!!