我正在开发一个Android应用程序,该程序必须在Google地图上显示多个GPS设备(例如手机)的实时位置。我正在使用Amazon Dynamo DB作为此应用程序的数据库。我已经成功构建了一个界面,驾驶员可以登录,并且其实时位置将存储在我的数据库中,即Dynamo Db。
现在,问题在于在地图上显示标记并设置动画效果。我有以下代码,通过SCAN操作提取Dynamo DB中存储的所有位置并尝试显示标记。
org.apache.jena
此后,我在一个计时器中调用此方法,该计时器每1秒重复一次。但是问题在于它不是在给标记设置动画,而是继续在一个标记上添加新标记。
答案 0 :(得分:0)
所以,终于有了一个解决方案,因为@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。最后一步 现在,您只需要在onCreate()方法中调用一次initialListCreation()方法即可,并且可以使用TimerTask以2000-3000(ms)的周期性间隔调用showMarker()。
仅此而已。享受吧!