如何从谷歌地图标记获取对象ID?

时间:2013-09-25 16:03:25

标签: java android google-maps hashmap android-maps-v2

我正在使用此代码在Google地图上绘制标记(隐藏屏幕上看不到的标记)

for (MyMapPointModel item : items) {
            // If the item is within the the bounds of the screen
            if (bounds.contains(item.getLatLng())) {
                // If the item isn't already being displayed
                if (!visibleMarkers.containsKey(item.getId())) {
                    // Add the Marker to the Map and keep track of it with
                    // the HashMap
                    // getMarkerForItem just returns a MarkerOptions object
                    customMarker = getMap().addMarker(getMarkerForItem(item));
                    visibleMarkers.put(item.getId(), customMarker);
                    drawMarker(item.getLatLng(), item.getThumbUri(), item.getId());
                }
            } else { // If the marker is off screen
                // If the course was previously on screen
                if (visibleMarkers.containsKey(item.getId())) {
                    // 1. Remove the Marker from the GoogleMap
                    visibleMarkers.get(item.getId()).remove();
                    // 2. Remove the reference to the Marker from the
                    // HashMap
                    visibleMarkers.remove(item.getId());

                }
            }
        }

我正在使用hashmap中的items id存储标记 我想调用一个带有录音标记细节的活动,我无法从onMarkerClick监听器获取项目ID(他只提供标记对象)。我错过了什么,如果我是什么?有没有人有更好的主意?

3 个答案:

答案 0 :(得分:1)

  

我正在使用hashmap中的items id存储标记   我正在使用hashmap

我假设您在其他地方成功使用该特定数据结构,因此它非常有用。

对于您的问题,听起来您需要HashMap<String, Integer>,使用getId()上的Marker作为密钥。这样您就可以查看Integer给出Marker

或者,如果您要创建自己的信息窗口,请使用Marker中的标题或摘要字段来保存整数的字符串表示形式,这样您就可以使用getTitle()或{{1}直接检索该值。

答案 1 :(得分:0)

尝试以下

 mMap.setOnMarkerClickListener(new OnMarkerClickListener()
                {

                    @Override
                    public boolean onMarkerClick(Marker arg0) {
                        arg0.getId() //get id
                        //use switch case
                        // or use title
                        if(arg0.getTitle().equals("title"))   
                        // do something
                        return true;
                    }

                });

答案 2 :(得分:0)

使用此代码

 mMap.setOnMarkerClickListener(new OnMarkerClickListener()
            {

                @Override
                public boolean onMarkerClick(Marker arg0) {
                    if(arg0.getTitle().equals("MyHome")) // if marker source is clicked

                    return true;
                }

            });