Search code examples
androidgoogle-mapshashmapgoogle-maps-markersinfowindow

infowindow not showing hashmap data


Hello I have some markers created form data in a listarray. These are created properly then I add the data to be shown in the infowindow of each marker to a hashmap.

But the infowindow does not seem to read the hashmap data, not sure where I'm going wrong.

The debugging tags I have all show that the data gets properly stored in the hashmap upon creating markers.

then in the infowindow the right markerid is recognized but the hashmap data linked to that makerid does not show, all markers show infowindow with same data.

public class MapFragment extends Fragment implements OnMapReadyCallback {

static final LatLng Loc2 = new LatLng(12.106011, -68.935108);
private GoogleMap map;
private ArrayList<Customer> customerList = new ArrayList<>();
private HashMap<String, HashMap<String, String>> outerMap = new HashMap<String, HashMap<String, String>>();
private HashMap<String, String> innerMap = new HashMap<String, String>();
private Marker marker;
final String TAG3 = "MapFragment:";

public MapFragment() {

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppStart appStart = (AppStart)getActivity();
    customerList = appStart.setMarkers();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_map, container, false);
    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    SupportMapFragment fragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
    fragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    map = googleMap;

    if (map != null) {
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            map.setMyLocationEnabled(true);
        }

        map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                marker.hideInfoWindow();
            }
        });

        //Create Markers & HashMap
        setMarker();

        map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                View v = getActivity().getLayoutInflater().inflate(R.layout.infowindow, null);
                TextView tvCName = (TextView) v.findViewById(R.id.tvCustomerName);
                TextView tvCAddress = (TextView) v.findViewById(R.id.tvCustomerAddress);
                TextView tvCTel = (TextView) v.findViewById(R.id.tvCustomerTel);

                HashMap<String, String> map = outerMap.get(marker.getId());
                Log.d(TAG3,"Info Window: get markerID just clicker: "+marker.getId());
                Log.d(TAG3,"Info Window: get name linked to markerID just clicked: "+map.get("Name"));

                tvCName.setText(map.get("Name"));
                tvCAddress.setText(map.get("Address"));
                tvCTel.setText(map.get("Tel"));
                return v;
            }
        });

        // Move the camera instantly to Curacao with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(Loc2, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
    }
}

private void setMarker(){
    for(Customer customer : customerList){
        String name = customer.getCompanyName();
        Double latitude = customer.getCompanyLatitude();
        Double longitude = customer.getCompanyLongitude();
        String address = customer.getCompanyAddress();
        String tel = customer.getCompanyTel();

        innerMap.put("Name", name);
        innerMap.put("Address",address);
        innerMap.put("Tel",tel);

        LatLng latLng = new LatLng(latitude, longitude);

        marker = map.addMarker(new MarkerOptions()
                .position(latLng)
                .title(name)
                .icon(BitmapDescriptorFactory.defaultMarker(120)));
        String markerID = marker.getId();
        outerMap.put(markerID, innerMap);
        Log.d(TAG3,"Create Marker: getName just saved: "+name);
        Log.d(TAG3,"Create Marker: getMarkerID just created: "+markerID);
        Log.d(TAG3,"Create Marker: getName jut saved in innerMap: "+innerMap.get("Name"));
        HashMap<String, String> map = outerMap.get(markerID);
        Log.d(TAG3,"Create Marker: get name just saved in innerMap and outerMap linked with marker just created: "+map.get("Name"));
    }
}
}

Solution

  • Just create a new instance of innerMap for each customer.

    Remove this line:

    private HashMap<String, String> innerMap = new HashMap<String, String>();
    

    And in your setMarker() method add this:

    Map<String, String> innerMap = new HashMap<String, String>();
    

    Your setMarker() method will look like this:

    private void setMarker(){
        for(Customer customer : customerList){
            //...
    
            Map<String, String> innerMap = new HashMap<String, String>(); // Add this line
    
            innerMap.put("Name", name);
            innerMap.put("Address",address);
            innerMap.put("Tel",tel);
    
            //...
        }
    }