gpt4 book ai didi

java - map 上多个标记的实时动画

转载 作者:行者123 更新时间:2023-12-02 10:12:33 29 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我必须在 Google map 上显示多个 GPS 设备(比如说手机)的实时位置。我使用 Amazon Dynamo DB 作为此应用程序的数据库。我已经成功构建了一个界面,驾驶员可以在其中登录,并且他们的实时位置将存储在我的数据库中,即 Dynamo Db。

现在,问题在于在 map 上显示标记并为其设置动画。我有以下代码,我通过 SCAN 操作获取 Dynamo DB 中存储的所有位置并尝试显示标记。

`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);
double fetchedLatitude;
double fetchedLongitude;
int fetchedVehicleId;
@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));
Log.e("Json String", OnTrackVehiclesString);
stringBuilder.append(OnTrackVehiclesString+"\n");
JsonModel onTrackVehiclesObject = gson.fromJson(OnTrackVehiclesString,JsonModel.class);
fetchedLatitude = onTrackVehiclesObject.get_latitude();
fetchedLongitude= onTrackVehiclesObject.get_longitude();
LatLng latLng = new LatLng(fetchedLatitude,fetchedLongitude);
if(marker == null){
mMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromBitmap(smallMarker)) );
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(fetchedLatitude, fetchedLongitude), 16.5f));
}
else{
MarkerAnimation.animateMarkerToICS(marker,latLng, new LatLngInterpolator.Spherical());
// mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(fetchedLatitude,fetchedLongitude),16.5f));
}
}
}
catch (Exception e){
Log.e("Exception caught",e.toString());
}
}
});
}
}).start();
}`

此后,我在计时器内调用此方法,该计时器每 1 秒后重复一次。但问题是它没有为标记设置动画,而是不断地在另一个标记上添加新标记。

最佳答案

所以,最后我得到了一个解决方案,正如 @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()。

仅此而已。享受吧!

关于java - map 上多个标记的实时动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54905184/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com