Android Google Maps, how to force each InfoWindow marker to open a different activity?

I am using the following code segment to display multiple locations on a google map. I am getting these coordinates as an array. After displaying the markers on the map, I want to go to activity after clicking the "Infoox" button of the marker. Each click of one marker should have a different activity after clicking on it. I have 4 markers and I want to access 4 different actions by clicking the infowindow button. How should I implement this. Thanks you

My map markers code

     Marker a1 = googleMap.addMarker(new MarkerOptions().position(a)
                .title(arr[0])
                .snippet(arr[1])            );
       Marker b1 = googleMap.addMarker(new MarkerOptions().position(b)
                .title(arr[9])
                .snippet(arr[10])                 );
        Marker c1= googleMap.addMarker(new MarkerOptions().position(c)
                .title(arr[18])
                .snippet(arr[19]));
        Marker d1= googleMap.addMarker(new MarkerOptions().position(d)
                .title(arr[27])
                .snippet(arr[28])); 

      

+2


source to share


2 answers


Use a HashMap to store the id of the marker and appropriately identify which activity it should open.

Then use OnInfoWindowClickListener

to get the event of the user who clicked the info window and use the HashMap to determine which activity to open.

Declare HashMap as an instance variable:

//Declare HashMap to store mapping of marker to Activity
HashMap<String, String> markerMap = new HashMap<String, String>();

      



Then, every time you add a marker, make an entry in the HashMap:

        String id = null;

        Marker a1 = googleMap.addMarker(new MarkerOptions().position(a)
                .title(arr[0])
                .snippet(arr[1]));

        id = a1.getId();
        markerMap.put(id, "a1");

        Marker b1 = googleMap.addMarker(new MarkerOptions().position(b)
                .title(arr[9])
                .snippet(arr[10]));

        id = b1.getId();
        markerMap.put(id, "b1");

        Marker c1= googleMap.addMarker(new MarkerOptions().position(c)
                .title(arr[18])
                .snippet(arr[19]));

        id = c1.getId();
        markerMap.put(id, "c1");

        Marker d1= googleMap.addMarker(new MarkerOptions().position(d)
                .title(arr[27])
                .snippet(arr[28]));

        id = d1.getId();
        markerMap.put(id, "d1");
    }

      

And then define an info window to listen for clicks:

    googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

            String m = markerMap.get(marker.getId());

            if (m.equals("a1")){
                Intent i = new Intent(MainActivity.this, ActivityA1.class);
                startActivity(i);
            }
            else if (m.equals("b1")){
                Intent i = new Intent(MainActivity.this, ActivityB1.class);
                startActivity(i);
            }
            else if (m.equals("c1")){
                Intent i = new Intent(MainActivity.this, ActivityC1.class);
                startActivity(i);
            }
            else if (m.equals("d1")){
                Intent i = new Intent(MainActivity.this, ActivityD1.class);
                startActivity(i);
            }
        }
    });

      

+2


source


I can see that several fields are related:

  • Name
  • Excerpt
  • Position
  • Activity

Here's how you can do it in code:



  • Create your own class:

    public static class MyMarker {
    
        Marker marker;
        MarkerOptions options = new MarkerOptions();
        Class activity;
    
        public MyMarker(String title, String snippet, LatLng position, Class activity) {
            // MarkerOptions
            options.title(title)
                    .snippet(snippet)
                    .position(position);
    
            // Target activity
            this.activity = activity;
        }
    
        // Compare Marker variable instead of MyMarker
        @Override
        public boolean equals(Object o) {
            return o != null && o.equals(marker);
        }
    }
    
          

  • Prepare markers:

    final List<MyMarker> myMarkers = new ArrayList<>();
    myMarkers.add(new MyMarker("title1", "snippet1", new LatLng(10, 10), MainActivity1.class));
    myMarkers.add(new MyMarker("title2", "snippet2", new LatLng(20, 20), MainActivity2.class));
    
          

  • Add markers to GoogleMap and save the returned object:

    for (MyMarker myMarker : myMarkers) {
        myMarker.marker = googleMap.addMarker(myMarker.options);
    }
    
          

  • Add a marker listener to your map:

    googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            // Will use the overridden equals method
            int index = myMarkers.indexOf(marker);
            if (index != -1) {
                // Considering that MainActivity is the current activity
                Intent intent = new Intent(MainActivity.this, myMarkers.get(index).activity);
                startActivity(intent);
            }
            return false;
        }
    });
    
          

The above listener will look for the click marker in your added marker array and, if there is one, will fire the associated activity.

0


source







All Articles