如何将动态数据传递给android中的自定义信息窗口适配器?

时间:2015-07-24 12:42:27

标签: android google-maps-api-3 google-maps-android-api-2

我正在尝试在自定义窗口适配器中传递动态数据(文本和图像drawables),但我得到所有标记的相同数据,我需要不同的文本和不同的图像到我各自的标记??如何获得? 我试过下面的代码。

CustomInfoWindowAdapter.class

public class CustomInfoWindowAdapter implements InfoWindowAdapter {

    private View v;
    String selectedPath;
    LatLng latLng;
    String addtext;
    Context context;

    CustomInfoWindowAdapter(Context context, String addtext, String selectedPath, LatLng latLng) {
        // TODO Auto-generated constructor stub
        this.selectedPath = selectedPath;
        this.latLng = latLng;
        this.addtext = addtext;
        this.context=context;

    }

    @Override
    public View getInfoContents(Marker marker) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        v = inflater.inflate(R.layout.infowindow,
                null);
        ImageView icon = (ImageView) v.findViewById(R.id.icon);
        // set some bitmap to the imageview
        if (selectedPath != "") {
            Log.d("Selectedpath", "sekectedpath" + selectedPath);
            icon.setImageURI(Uri.parse(selectedPath));
        }
        TextView txt = (TextView) v.findViewById(R.id.textView1);
        txt.setText(addtext);

        return v;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        // TODO Auto-generated method stub
        return null;
    }

}

我将CustomInfoWindowAdapter调用到我的活动,如下所示

        googleMap.setInfoWindowAdapter(new MyInfoWindowAdapter(addtext, selectedPath, latLng));

1 个答案:

答案 0 :(得分:2)

  

添加标记以及特定于其的信息。当您点击特定的InfoWindow时,您将获得getInfoContents(标记标记)方法中的特定标记数据。

 public class MainActivity extends AbstractMapActivity implements
 OnMapReadyCallback, OnInfoWindowClickListener {
 private boolean needsInit=false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 if (readyToGo()) {
  setContentView(R.layout.activity_main);

  MapFragment mapFrag=
      (MapFragment)getFragmentManager().findFragmentById(R.id.map);

  if (savedInstanceState == null) {
    needsInit=true;
  }

  mapFrag.getMapAsync(this);
 }
 }

 @Override
 public void onMapReady(final GoogleMap map) {
 if (needsInit) {
  CameraUpdate center=
      CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
                                               -73.98180484771729));
  CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

  map.moveCamera(center);
  map.animateCamera(zoom);
  }
  map.addMarker(new MarkerOptions()
 .position(new LatLng(37.7750, 122.4183))
 .title("San Francisco")
 .snippet("Population: 776733"));

   map.addMarker(new MarkerOptions()
 .position(new LatLng(37.7750, 122.4183))
 .title("San Francisco")
 .snippet("Population: 776733"));


   map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
   map.setOnInfoWindowClickListener(this);
  }

 @Override
 public void onInfoWindowClick(Marker marker) {
 Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
 }

 private void addMarker(GoogleMap map, double lat, double lon,
                     int title, int snippet) {
 map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                                 .title(getString(title))
                                 .snippet(getString(snippet)));
 }
 }
  

请参阅此https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Popups