How do I pass dynamic data to a custom info window adapter in android?

I am trying to pass dynamic data (text and graphics plots) to a custom window adapter, but I get the same data for all markers, do I need different text and different images for my respective markers? I've tried the code below.

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

}

      

and i called CustomInfoWindowAdapter for my activity like below

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

      

+3


source to share


1 answer


Add markers with information related to her. and when you click on a specific InfoWindow, you will get the specific marker data in the getInfoContents (Marker) method.

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

      



Refer to https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Popups

+2


source







All Articles