Associate an object with Marker (google map v2)

前端 未结 3 1286
梦谈多话
梦谈多话 2020-12-24 12:26

In my app I have some objects that have their location displayed on the map using markers. The problem is that the only way I\'ve found to handle marker clicks is

         


        
相关标签:
3条回答
  • 2020-12-24 12:36

    I reckon this callback was not very thoroughly though by the Android team, but, it's what we have.

    Whenever you call mMap.addMarker(); it returns the generated marker. You can then use a HashMap or some other data holder structure to remember it.

    // Create the hash map on the beginning
    WeakHashMap <Marker, Object> haspMap = new WeakHashMap <Marker, Object>();
    
    
    // whenever adding your marker
    Marker m = mMap.addMarker(new MarkerOptions().position(lat, lng).title("Hello World").icon(icon_bmp));
    haspMap.put(m, your_data);
    
    0 讨论(0)
  • 2020-12-24 12:40

    You can associate arbitrary object by using Marker's setTag() method

    Marker amarker = mMap.addMarker(new MarkerOptions().position(lat, lng).title("Hello World"));
    amarker.setTag(new SomeData());
    

    To retrieve data associated with marker, you simply read it using its getTag() and then cast it to its original type.

    SomeData adata = (SomeData) amarker.getTag();
    

    More information

    0 讨论(0)
  • 2020-12-24 12:42

    Another option would be to create a Map whose keys is marker.getId() and the value is our object.

    In this way, we wouldn't keep a reference to a Marker in memory, and wouldn't have to worry about garbage collected markers.

    Here you can see more answers.

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