Search code examples
androidgoogle-mapsgoogle-maps-api-2

Android-Google Maps V2-Adding custom infowindows to markers created from an array


I want to create an app which can receive a set of points from a database and add markers for each point on a map. I am currently using an object array for testing purposes. I have used the same marker variable in a loop to put the markers on the map.

The array which contains my marker positions also contains some other data about those specific locations which I want to bring up in a custom infowindow (which gets its data from the array) when the markers are touched.

I am unsure of how I should differentiate between the different markers in calling this infowindow.

OnMarkerClickListener document on developers.google.com

My code for adding markers:

for (pointNumber= 0; pointNumber<pointArray.length; pointNumber++) {
    taxiLatitude = pointArray[pointNumber].position.latitude;
    taxiLongitude = pointArray[pointNumber].position.longitude;

    if (valueInArray<someValue) {
        pointMarker = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(pointArray[pointNumber].position.latitude, carsArray[pointNumber].position.longitude))
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
            .title("Title: "+pointArray[pointNumber].pointTitle)
            .snippet("Snippet: "+pointArray[pointNumber].pointSnippet));
    } else {
        System.out.println("Out of range");
    }
}

My code to add data to my custom infowindow, which I got here

class CustomWindowAdapter implements GoogleMap.InfoWindowAdapter {
    LayoutInflater mInflater;
    public CustomWindowAdapter(LayoutInflater i){
        mInflater = i;
    }

    @Override
    public View getInfoContents(Marker marker) {
        // Getting view from the layout file
        View v = mInflater.inflate(R.layout.custom_window_layout, null);
        // Populate fields
        ImageView image = (ImageView) v.findViewById(R.id.image);
        image.setImageResource(pointArray[pointNumber].image);

        TextView title = (TextView) v.findViewById(R.id.tv_1);
        title.setText(pointArray[pointNumber].text1);

        TextView description = (TextView) v.findViewById(R.id.text_2);
        description.setText(pointArray[pointNumber].text2);
        }
        // Return info window contents
        return v;      

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }
}

Solution

  • You have to implement a HashMap, realx, is easy, I'll show you how do that to identify all your markers:

    Create the HashMap with key type String and object type Marker:

    HashMap<String,Marker> hashMarkers = new HashMap<>();
    

    Add the markers to the hash map identified by number (converted to string):

    for (int x= 0; x < yourArray.length; x++) {
            hashMarkers.put(String.valueOf(x), mMap.addMarker(new MarkerOptions()
                    .position( your array (or arrays) of positions(s) )
        }
    

    Then you can identify every marker you want:

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(Marker marker) {
                        if (marker.equals(hashMarkers.get("the number of the marker"))) {
                            //here you can call your custom infoWindow or set the content 
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
    

    Hope this helps you.