Android Google Maps, how to make each Marker InfoWindow open different Activity?

前端 未结 2 1157
太阳男子
太阳男子 2020-11-27 08:22

I use following code segment for display several locations on google map. I get those coordinates as a array. After displaying markers on the map i want to go for a activity

相关标签:
2条回答
  • 2020-11-27 08:55

    Use a HashMap to store the marker ID and it's corresponding identification of which Activity it should open.

    Then, use a OnInfoWindowClickListener to get the event of a user clicking the info window, and use the HashMap to determine which Activity to open.

    Declare the HashMap as an instance variable:

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

    Then, each 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 the info window click listener:

        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);
                }
            }
        });
    
    0 讨论(0)
  • 2020-11-27 08:59

    I can see that multiple fields are linked:

    • Title
    • Snippet
    • Position
    • Activity

    Here's how you can accomplish this in code:

    1. Create a custom 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);
          }
      }
      
    2. 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));
      
    3. Add markers to GoogleMap and save the returned object:

      for (MyMarker myMarker : myMarkers) {
          myMarker.marker = googleMap.addMarker(myMarker.options);
      }
      
    4. Add a marker click 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 clicked marker in your added markers array and if it's there, will start the associated activity.

    0 讨论(0)
提交回复
热议问题