【问题标题】:Add different layout of infoWindow to marker将 infoWindow 的不同布局添加到标记
【发布时间】:2017-07-25 22:22:46
【问题描述】:

我的地图上有几个标记。对于他们每个人,我都想为他们添加一个自定义的 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。有没有人对如何识别标记有一个很好的解决方案,以便我可以为不同的布局充气?

【问题讨论】:

  • 每个标记都有 lat long,因此在标记单击时,您可以通过比较标记 lat long 来生成布局。
  • @chetanprajapat 太好了,它有效!非常感谢
  • 永远欢迎...

标签: java android google-maps infowindow


【解决方案1】:

您可以使用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
 }

【讨论】:

    【解决方案2】:

    没有看到这个我觉得有点愚蠢,但我设法写了一个解决方案。 感谢@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;
                }
        }
    

    然后我只是将适配器设置为CustomInfoWindowAdapter 的新Instance

    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
    

    再次感谢@chetanprajapat 的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-23
      • 2012-02-01
      • 2021-05-28
      • 2019-09-16
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2012-08-18
      相关资源
      最近更新 更多