Google 地图中每个地图标记的对话框片段?

Dialog Fragment for each Map Marker in Google Maps?

我有一个对话框片段,它是通过 google 地图中的 InfoWindowClickListener 打开的。我需要为地图上的每个标记创建一个独特的对话片段。对话片段的创建如下,对于每个地方,在一个循环中顺序:

public PlaceMarker(Place place) {
            this.place = place;
            this.marker = map.addMarker(new MarkerOptions()
                    .position(place.getLocation())
                    .title(this.place.getName())
                    .snippet(place.getInfo()));
            mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    FragmentManager manager = getFragmentManager();
                    //restdialog = RestaurantDialog.newInstance(place);
                    RestaurantDialog restdialog = new RestaurantDialog();
                    Bundle args = new Bundle();
                    args.putSerializable("place", place);
                    restdialog.setArguments(args);
                    restdialog.setTargetFragment(DefaultMap.this, 0);
                    restdialog.show(manager, place.getName());
                }
            });
        }

我遇到的问题是,虽然标记信息 windows 完全符合我的预期(每个地方上面都有自己的详细信息),但每次单击标记的对话框片段都显示为最近创建的对话框片段(如果这没有意义请告诉我)。即,如果我按以下顺序为以下地点创建地标:巴尔的摩、费城、华盛顿特区,巴尔的摩、费城和华盛顿特区的信息 windows 将具有每个地点的正确信息 window当你点击他们的标记时,你会看到他们的位置,但是点击信息 window 你只会看到所有 3 个地方的华盛顿特区的对话片段。

编辑:我通过这段代码添加了 for 循环中的位置,它取代了位置并为每个位置创建了一个新的 restaurantmarker:

public void addPlace(Place place) {
    this.places.add(place); //this.places is a List<Place>
    this.markerBank.put(place, new PlaceMarker(place)); //this.markerBank is a HashMap <Place, PlaceMarker>
}

您的问题是您将最后一个位置存储在名为 Place 的变量中,并使用以下行:

this.place = place;

当您的 onInfoWindowClick 触发时,它会使用该变量中的值。您需要有一个地点列表并将每个地点与每个标记相关联。为此使用 HashMap。类似于:

HashMap<Place, Marker> myList = new HashMap<>();

希望对您有所帮助。