How to link Google Maps Android API v2 Marker to an object

后端 未结 2 1697
一整个雨季
一整个雨季 2021-01-07 09:13

I\'m adding dynamically a non-fixed amount of markers in a map, which each of them are related to one instance of my POCO class.

I need to link them so when the user

2条回答
  •  太阳男子
    2021-01-07 09:39

    I know this post is old, but if you are using the prefab map Activity in Android studio

    In the set up map

      private void setUpMap() {
    
    
        MapmarkerInfoList = new HashMap();
    
      // get the marker Id as String
           String id =  mMap.addMarker(new MarkerOptions().position(new LatLng(/*set Latitude*/,  /*setLongitude*/).title("Marker")).getId();
           //add the marker ID to Map this way you are not holding on to  GoogleMap object
            markerInfoList.put(id,mapppedHouses.get(i));     
    }
    

    Then in the :

      private void setUpMapIfNeeded() {
      ///...
     if (mMap != null) {
       //if a marker is clicked
       mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                    @Override
                    public void onInfoWindowClick(Marker marker) {
                        someObject = markerInfoList.get(marker.getId());
                    }
                });
      }
     }
    

提交回复
热议问题