将标记添加到散列映射而不将其添加到地图

时间:2016-03-10 15:47:55

标签: java android google-maps google-maps-api-3

我想将google地图标记添加到此hashmap:

private HashMap<Marker, MyMarker> mMarkersHashMap;

不将它们添加到谷歌地图。这是我目前的代码:

MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);

此代码将标记添加到hashmap,但也将标记添加到地图(使用addMarker),这是我不想要的。任何建议

这是我正在处理的功能:

private void plotMarkers(ArrayList<MyMarker> markers)
    {
        if(markers.size() > 0)
        {
            for (final MyMarker myMarker : markers)
            {
                MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));

                Marker currentMarker = mMap.addMarker(markerOption);
                //Passes markers into hashmap so they can be used by the information window methods
                mMarkersHashMap.put(currentMarker, myMarker);

                mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                    @Override
                    public void onInfoWindowClick(Marker marker) {
                        Intent ListIntent = new Intent(getApplicationContext(), InfoWindowList.class);
                        MyMarker myMarker = mMarkersHashMap.get(marker);
                        String title = myMarker.getmLabel();
                        ListIntent.putExtra("COUNTY", title);
                        startActivity(ListIntent);
                    }
                });

                mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
            }

        }
        mClusterManager.addItems(markers);
    }

我希望mClusterManager.addItems(markers);将标记添加到地图中。

这是我的信息窗口函数,它需要hashmap来设置hashmap中标记的信息窗口:

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter
    {
        public MarkerInfoWindowAdapter()
        {
        }

        @Override
        public View getInfoWindow(Marker marker)
        {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker)
        {

            View v  = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
            MyMarker myMarker = mMarkersHashMap.get(marker);

            ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
            TextView markerLabel = (TextView)v.findViewById(R.id.marker_label);
            TextView anotherLabel = (TextView)v.findViewById(R.id.another_label);
            //anotherLabel.setOnClickListener(newsfeed);

            markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));
            anotherLabel.setText("Newsfeed");
            markerLabel.setText(myMarker.getmLabel());

            return v;

        }
    }

MyMarker课程:

public class MyMarker implements ClusterItem {
    private String mLabel;
    private String mIcon;
    private Double mLatitude;
    private Double mLongitude;
    private final LatLng mPosition;

    public MyMarker(String label, String icon, Double latitude, Double longitude)
    {
        this.mLabel = label;
        this.mLatitude = latitude;
        this.mLongitude = longitude;
        this.mIcon = icon;
        mPosition = new LatLng(latitude, longitude);
    }

    //mPosition = LatLng(mLatitude, mLongitude);
    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public String getmLabel()
    {
        return mLabel;
    }

    public void setmLabel(String mLabel)
    {
        this.mLabel = mLabel;
    }

    public String getmIcon()
    {
        return mIcon;
    }

    public void setmIcon(String icon)
    {
        this.mIcon = icon;
    }

    public Double getmLatitude()
    {
        return mLatitude;
    }

    public void setmLatitude(Double mLatitude)
    {
        this.mLatitude = mLatitude;
    }

    public Double getmLongitude()
    {
        return mLongitude;
    }

    public void setmLongitude(Double mLongitude)
    {
        this.mLongitude = mLongitude;
    }
}

2 个答案:

答案 0 :(得分:1)

当您将MarkerOptions提供给地图并致电addMarker时,会创建“标记”,这样您就无法先将标记添加到地图中,无法获得标记。

如果您只是想让标记可见,那么请更改其可见性

答案 1 :(得分:0)

您可以将标记添加到地图中,然后使用

隐藏它
currentMarker.setVisible(false);

不确定这是最有效的途径,但取决于您的用例。

根据提交进行编辑

如果您需要保留HashMap个标记,您是否尝试使用ClusterRenderer

以下是一个例子:

clusterManager.setRenderer(new DefaultClusterRenderer<MyMarker>(activity, map, clusterManager) {
            @Override
            protected void onClusterItemRendered(MyMarker myMarker, Marker marker) {
                super.onClusterItemRendered(mobileMarker, marker);
                // add to hashmap here
            }

            ... override other methods as needed

        });

clusterManager.addItems(markers)