2016-03-10 28 views
0

我想谷歌地圖標記添加此HashMap:添加標記到一個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; 
    } 
} 
+0

只是不添加標記,以你映射只有到HashMap中,你可以發表你的類的更多的代碼。 –

+0

我知道這是我必須做的,我想知道它是如何完成的。是的,我現在將添加更多的代碼 – Cian

回答

0

您可以將標記添加到地圖,但隱藏它使用

currentMarker.setVisible(false); 

不確定這是最有效的rou te,儘管..取決於你的用例。


基於commments

如果保持標記的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) 
+0

當我點擊地圖時,這給我一個錯誤。嘗試在空對象引用上調用虛擬方法'java.lang.String com.example.cillin.map.MyMarker.getmIcon()' – Cian

+0

您可以共享「MyMarker」類嗎? – Brad

+0

是的。我把它添加到我的問題的結尾。 – Cian

1

「標記」創建時你給MarkerOptions地圖和調用addMarker所以你如果不首先它首先添加到地圖中獲得的標記。

,如果你只是想不會有明顯的標記,然後更改其可見

+0

當我點擊地圖時,這給我一個錯誤。試圖調用虛擬方法'java.lang.String com.example.cillin.map.MyMarker.getmIcon()'對空引用 – Cian

+0

顯示你的代碼,getmIcon做什麼? – tyczj

+0

它用於獲取信息窗口的圖標(圖像)。我已將代碼添加到問題中。 – Cian