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

时间:2017-07-25 13:04:37

标签: java android google-maps infowindow

我的地图上有几个标记。对于他们中的每一个,我想要给自定义的infoWindow充气。

我遇到的问题是每个信息的infoWindow都是一样的。我已经阅读了几个堆栈线程,但我还没弄清楚如何修复它。

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

        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);
        }

充气自定义infoWindow的方法

    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。有没有人有一个很好的解决方案,如何识别标记,以便我可以膨胀不同的布局?

2 个答案:

答案 0 :(得分:0)

您可以使用marker.setTag()和marker.getTag()。您可以为marker提供标记。例如

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
 }

答案 1 :(得分:0)

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

由于我拥有关于标记(位置,标题)的所有信息,我可以将其传递给创建的类,该类将检查标题,然后为正确的布局充气。

用于充气正确布局的创建类

    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;
            }
    }

然后我将适配器设置为Instance

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

再次感谢@chetanprajapat的帮助。