8

我使用here的Google Maps Android標記羣集實用程序,我從我的服務器收到的列表中添加了一堆項目(約700)。我已經解決了最初的錯誤,並且可以看到這些簇,然後放大它們,並將它們視爲單個標記。但是,有一些項目與其他項目相距甚遠,所以即使在最大縮小範圍內,它們也不會聚集在一起。出於某種原因,這些項目不會顯示在我的地圖上 - 不是當我放大時,而是縮小時。Android地圖ClusteredMarkers - 不顯示非集羣?

我檢查了座標,它們是真實的,在我開始使用集羣之前,我可以看到這些項目沒有問題,所以我假設我在集羣代碼中做了一些錯誤。

下面是一些代碼:

private void setUpClusterer() { 
    // Initialize the manager with the context and the map 
    mClusterManager = new ClusterManager<>(getActivity(), map); 
    vendorRenderer = new VendorRenderer(); 
    mClusterManager.setRenderer(vendorRenderer); 
    mClusterManager.setOnClusterClickListener(this); 
    mClusterManager.setOnClusterItemClickListener(this); 
    //point the maps listeners at the listeners implemented by the cluster manager 
    map.setOnCameraChangeListener(mClusterManager); 
    map.setOnMarkerClickListener(mClusterManager); 

    //add items to the cluster manager 
    addClusterItems(-1); 
    mClusterManager.cluster(); 
} 

private void addClusterItems(int positionToMark) { 
    if (null == list) { 
     return; 
    } 
    LatLng position; 
    int maxMarkers = Math.min(list.size(), getResources().getInteger(R.integer.number_of_results_on_map)); 

    mClusterManager.clearItems(); 

    for (int i = 0; i < maxMarkers; i++) { 

     vendorItem = list.get(i); 

     if (vendorItem.getAddress().contains("Remote 1")) { 
      Log.e("Kibi", "Adding Remote 1, pos = " + i); 
      Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString()); 
     } 
     if (vendorItem.getAddress().contains("Clustered 1")) { 
      Log.e("Kibi", "Adding Clustered 1, pos = " + i); 
      Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString()); 
     } 
     if (vendorItem.getAddress().contains("Remote 2")) { 
      Log.e("Kibi", "Adding Remote 2, pos = " + i); 
      Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString()); 
     } 
     VendorMapItem item = new VendorMapItem(vendorItem.getPointCoordinates(), 
       "Some other text"); 
     if (i == positionToMark) { 
      selectedItem = item; 
     } 

     mClusterManager.addItem(item); 
    } 
    if (-1 == positionToMark) { 
     selectedItem = null; 
    } 
} 

這表明越來越添加的項目 - 我加入幫我看看,我的2個遠程項目具有良好的座標加入,並期待類似(雖然遠離)的日誌我所選擇的羣集的項(這被視爲)

這裏是渲染器的代碼:

/* This draws the markers for us */ 
private class VendorRenderer extends DefaultClusterRenderer<VendorMapItem> { 
    Context context = getActivity().getApplicationContext(); 
    public final IconGenerator mIconGenerator = new IconGenerator(context); 
    public final IconGenerator mSelectedIconGenerator = new IconGenerator(context); 
    private final View mItemView; 
    private final View mSelectedItemView; 

    public VendorRenderer() { 
     super(getActivity().getApplicationContext(), map, mClusterManager); 

     // Create selected custom Marker 
     RelativeLayout selectedContainer = (RelativeLayout) view.findViewById(R.id.marker_map_selected_container); 
     mSelectedItemView = ((LayoutInflater) getActivity().getSystemService 
       (Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_marker_selected_layout, selectedContainer, false); 
     mSelectedIconGenerator.setContentView(mSelectedItemView); 
     mSelectedIconGenerator.setBackground(null); 

     // Create custom Marker 
     LinearLayout container = (LinearLayout) view.findViewById(R.id.text_marker_map_container); 
     mItemView = ((LayoutInflater) getActivity().getSystemService 
       (Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_marker_layout, container, true); 

     mIconGenerator.setContentView(mItemView); 
     mIconGenerator.setBackground(null); 
    } 

    @Override 
    protected void onBeforeClusterItemRendered(VendorMapItem vendor, MarkerOptions markerOptions) { 
     // Draw a single vendor. 
     Bitmap icon; 
     if (null == selectedItem || !vendor.getPosition().equals(selectedItem.getPosition())) 
     { 
      icon = mIconGenerator.makeIcon(); 
     } else { 
      icon = mSelectedIconGenerator.makeIcon(); 
     } 
     markerOptions.title(vendor.getTitle()); 
     markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)); 
    } 

    @Override 
    protected void onBeforeClusterRendered(Cluster<VendorMapItem> cluster, MarkerOptions markerOptions) { 
     // Draw multiple vendors clustered... 
     Bitmap icon = mIconGenerator.makeIcon(); 
     markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)); 
    } 

    @Override 
    protected boolean shouldRenderAsCluster(Cluster cluster) { 
     // Always render clusters. 
     return cluster.getSize() > 1; 
    } 
} 

一般而言羣集項目被示出,並且也declustered的,是否SEL是否受到影響。我有另一個視圖顯示列表中的所有位置數據,而我的遠程位置在那裏顯示的只是鰭。

任何想法我做錯了什麼?

回答

1

愚蠢。

基本上我多次調用map.clear()。我猜如果地圖上繪製的所有內容都是由聚合器控制的引腳,則不需要清除。一旦清除()通話被刪除,我的引腳再次出現。

0

公共類ClusterIconProvider實現IconDataProvider {

private static final int[] res = {R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5}; 
private static final int[] forCounts = { 10, 100, 1000, 10000, Integer.MAX_VALUE }; 
private Bitmap[] baseBitmaps; 
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
private Rect bounds = new Rect(); 
private MarkerOptions markerOptions = new MarkerOptions().anchor(0.5f, 0.5f); 


public ClusterIconProvider(Resources resources){ 
    baseBitmaps = new Bitmap[res.length]; 
    for (int i = 0; i < res.length; i++) { 
     baseBitmaps[i] = BitmapFactory.decodeResource(resources, res[i]); 
    } 
    paint.setColor(Color.WHITE); 
    paint.setTextAlign(Align.CENTER); 
    paint.setTextSize(resources.getDimension(R.dimen.Cluster)); 
} 
@Override 
public MarkerOptions getIconData(int markersCount) { 
    // TODO Auto-generated method stub 
    Bitmap base; 
    int i = 0; 
    do { 
     base = baseBitmaps[i]; 
    } while (markersCount >= forCounts[i++]); 
    Bitmap bitmap = base.copy(Config.ARGB_8888, true);  
    String text = String.valueOf(markersCount); 
    paint.getTextBounds(text, 0, text.length(), bounds); 
    float x = bitmap.getWidth()/2.0f; 
    float y = (bitmap.getHeight() - bounds.height())/2.0f - bounds.top; 
    Canvas canvas = new Canvas(bitmap); 
    canvas.drawText(text, x, y, paint); 
    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap); 
    return markerOptions.icon(icon); 
} 

}

+0

您可以用於在地圖上添加集羣 –

+0

感謝@Pratik,但是我已經使用了源中的集羣機制。當它們從不聚集在一起時,它不會繪製非聚集的單個項目,我無法弄清楚原因。 – Kibi