2017-07-25 52 views
0

我在地图上有几个标记。对他们中的每一个,我想膨胀一个自定义的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); 
     } 

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

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是一个二传手,因此覆盖了信息窗口每次for循环迭代。有没有人有如何识别标记的好解决方案,以便我可以膨胀不同的布局?

+1

你对每个标记都有很长的时间,所以在标记点击时,你可以通过比较标记lat long来生成你的布局。 –

+0

@chetanprajapat伟大的,它的作品!非常感谢 – Viktor

+0

随时欢迎... –

回答

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

类,然后我就适配器设置为一个新的InstanceCustomInfoWindowAdapter

mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter()); 

再次感谢@chetanprajapat的帮助。

0

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