将不同布局的 infoWindow 添加到标记

Add different layout of infoWindow to marker

我的地图上有几个标记。对于他们每个人,我想膨胀一个自定义信息窗口。

我遇到的问题是每个窗口的信息窗口都相同。我已经阅读了几个堆栈线程,但我还没有想出如何修复它。

我将标记添加到地图的片段

        for (int i = 0; i<cityObjects.size(); i++){
            CityObject cObject = cityObjects.get(i);
            Coordinates loc = cObject.getCoordinates();
            LatLng pos = new LatLng(loc.getLatitude(), loc.getLongitude());
            mMap.addMarker(new MarkerOptions().position(pos).title(cObject.getName()));
            loadInfoWindow(cObject.getImgs().get(0), cObject.getName());

            builder.include(pos);
        }

膨胀自定义信息窗口的方法

    public void loadInfoWindow(final String url, final CharSequence title) {
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {


            @Override
            public View getInfoWindow(Marker arg0) {
                arg0.getId();
                View v = getActivity().getLayoutInflater().inflate(R.layout.layout_info_window, null);
                Button info = (Button) v.findViewById(R.id.infoButton);
                info.setText(title);
                BitmapLayout back = (BitmapLayout) v.findViewById(R.id.bitmapBackground);
                Picasso.with(getContext()).load(url).into(back);

                return v;

            }

            @Override
            public View getInfoContents(Marker arg0) {

                return null;
            }
        });

    }

我读到一些关于 setInfoWindowAdapter 是 setter 的内容,因此每次 for 循环迭代时都会覆盖 infoWindow。有没有人有关于如何识别标记的好解决方案,以便我可以膨胀不同的布局?

您可以使用 marker.setTag() 和 marker.getTag()。您可以给 marker.for 示例

一个标签
mMap.addMarker(new MarkerOptions().position(pos).title(cObject.getName())).setTag(1);

mMap.addMarker(new MarkerOptions().position(pos).title(cObject.getName())).setTag(2);

然后在 loadInfoWindow 方法中

   if((int)arg0.getTag==1)
  {
  //do something
  }
 else{
 //do something
 }

没有看到这个我觉得有点傻,但我设法写了一个解决方案。 感谢@chetanprajapat 让我走上正轨。

因为我有关于标记(位置、标题)的所有信息,所以我可以将其传递到创建的 class 中,它将检查标题,然后展开正确的布局。

已创建 class 用于膨胀正确的布局

    public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter{
            @Override
            public View getInfoWindow(Marker arg0) {

                for (CityObject cityObject : cityObjects){
                    if (arg0.getTitle().equals(cityObject.getName())){
                        View v = getActivity().getLayoutInflater().inflate(R.layout.layout_info_window, null);
                        Button info = (Button) v.findViewById(R.id.infoButton);
                        info.setText(cityObject.getName());
                        BitmapLayout back = (BitmapLayout) v.findViewById(R.id.bitmapBackground);
                        Picasso.with(getContext()).load(cityObject.getImgs().get(0)).into(back);
                        return v;
                    }
                }
                return null;
            }

            @Override
            public View getInfoContents(Marker arg0) {

                return null;
            }
    }

然后我将适配器设置为 CustomInfoWindowAdapter

的新 Instance
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

再次感谢@chetanprajapat 的帮助。