How to set tag to Google Map's marker in Android?

前端 未结 3 1589
傲寒
傲寒 2021-02-03 22:12

I\'m trying to set onInfoWindowClickListener for each marker that hit the related API according to the POJO id. That\'s what I found for settin

相关标签:
3条回答
  • 2021-02-03 22:21

    I have one way. Just create HashMap with both your main Marker and another one with POJO Class and get back your POJO Class based on Particular Marker clicked on getInfoContents(.....)

    0 讨论(0)
  • 2021-02-03 22:23

    Try to call setTag() method on map marker, not on the MarkerOptions object for.e.g.,

    private void addMk(LatLng latLng, String title, String snips,String tagId) {
     MarkerOptions options = new MarkerOptions()
                    .position(latLng)
                    .title(title)
                    .snippet(snips);
            
                     googleMap                 //Google googleMap object for map reference
                    .addMarker(options)
                    .setTag(tagId);
    
        }
    
    googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    
            @Override
            public void onInfoWindowClick(Marker marker) {
                // TODO Auto-generated method stub
                  
                   String tagId= String.valueOf(marker.getTag());
                    Log.d(TAG, "onInfoWindowClick: TAG_ID "+tagId);
            }
        }); 
    

    Hope, this works!

    0 讨论(0)
  • 2021-02-03 22:46

    Now You can do like this

    Update your google play service version to

    com.google.android.gms:play-services:9.4.0
    
    Marker marker=mMap.addmarker(new MarkerOptions(LatLang));
    // set object as tag
    marker.setTag(your object)
    
    // to retrieve the marker
    marker.getTag();// Type cast to your object type;
    
    0 讨论(0)
提交回复
热议问题