gpt4 book ai didi

java - 如何在Arraylist上正确添加Google Map上的添加标记

转载 作者:行者123 更新时间:2023-12-02 12:23:42 25 4
gpt4 key购买 nike

我在如何在 Arraylist 上添加 addMarker 时遇到问题。当我在 for 循环上使用 createMarker 来添加值时。

MainActivity extends BaseActivityLocation
implements NavigationView.OnNavigationItemSelectedListener, SearchView.OnQueryTextListener,
SearchView.OnSuggestionListener {

@Override
public boolean onQueryTextSubmit(String query) {
for(Marker m : list) {
System.out.println(list+m.getSnippet() + m.getTitle() + m.getPosition() + " snippet");
if(m.getSnippet().toLowerCase().equals(query)) {
Toast.makeText(this, "You searched for: " + query , Toast.LENGTH_SHORT).show();
mMapFragment.animateCamera(CameraUpdateFactory.newLatLngZoom(m.getPosition(), 9.0f));
break; // stop the loop
}
}
return false;
}

public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
// success response
if (response.body().isJsonArray()) {
JsonArray objectWhichYouNeed = response.body().getAsJsonArray();
System.out.println(objectWhichYouNeed + " Object");

for (int i = 0; i < response.body().getAsJsonArray().size(); i++) {
JsonElement plate_num_array = response.body().getAsJsonArray().get(i);
JsonObject plate_num_obj = plate_num_array.getAsJsonObject();
String plate_n = plate_num_obj.get("plate_num").toString();
String plate_nString = plate_n;
plate_nString = plate_nString.replace("\"", "");
String plate_num = String.valueOf(plate_nString);

JsonElement lat_array = response.body().getAsJsonArray().get(i);
JsonObject lat_obj = lat_array.getAsJsonObject();
String lati = lat_obj.get("lat").toString();
String latiString = lati;
latiString = latiString.replace("\"", "");
String lat = String.valueOf(latiString);

JsonElement lng_array = response.body().getAsJsonArray().get(i);
JsonObject lng_obj = lng_array.getAsJsonObject();
String longi = lng_obj.get("lng").toString();
String longiString = longi;
longiString = longiString.replace("\"", "");
String lng = String.valueOf(longiString);

createMarker(d, d2, plate_num);
}

这里的问题是,当调用 createMarker 时,标记将仅替换其在 Arraylist 上的值。我想要的是在数组列表中添加标记,例如 [0,0,0,0]..我该如何实现这一点?

 public MarkerOptions createMarker(Double latitude, final Double longitude, String Plate_num) {
BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.bus);

list = new ArrayList<>();

System.out.print(list + " Snippp");

/* mMapFragment.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title("Plate No.")
.snippet(Plate_num)
.icon(image));*/

mMapFragment.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
marker.hideInfoWindow();
}
});

mMapFragment.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 9.0f));

mMapFragment.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

@Override
public boolean onMarkerClick(final Marker marker) {
vm = new VehicleMap();

vm.setSnippet(marker.getSnippet());
vm.setPlate_num(marker.getSnippet());

latitudeG = marker.getPosition().latitude;
longitudeG = marker.getPosition().longitude;

marker.showInfoWindow();

BottomSheetDialogFragment bottomSheetDialogFragment = new BottomSheetModalFragment(activity);
bottomSheetDialogFragment.show(getFragmentManager(), bottomSheetDialogFragment.getTag());

return true;
}
});

return new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Plate No.")
.snippet(Plate_num)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

}

最佳答案

它不是覆盖一个值,而是替换整个列表。

不要创建新列表。让方法创建并返回。

public MarkerOptions createMarker(Double latitude, final Double longitude, String Plate_num) {

// list = new ArrayList<>(); // Don't do this

BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.bus);

return new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Plate No.")
.snippet(Plate_num)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}

...

onResponse( ... ) {
...
String lng = String.valueOf(longiString);

MarkerOptions opts = createMarker(d, d2, plate_num);
Marker m = mMapFragment.addMarker(opts);
list.add(m);
}

关于java - 如何在Arraylist上正确添加Google Map上的添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45581201/

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