2013-05-09 50 views
0

我在谷歌地圖中的自定義標記有問題。我會盡力解釋。如何從谷歌地圖V2中刪除CustomMarker android

我有一些標記,並且我做了一個Asyntask,如果必要的話,它將它聚類,返回一個LinkedHashMap<Point, ArrayList<MarkerOptions>> clusters我擁有這些簇的地方。每個位置代表一個集羣(它可能有1個標記集羣)

當我拿到這個名單,我的集羣添加到地圖:

這裏就是我所說到Clusterizer活動。

protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     map.getUiSettings().setMyLocationButtonEnabled(true); 

     CameraUpdate camUpd1 = CameraUpdateFactory.newLatLngZoom(new LatLng(41.40520680710329,2.191342011603923),MAP_ZOOM_LEVEL);  
     map.animateCamera(camUpd1); 

     loadMarkers(); 

     map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() { 

      @Override 
      public void onCameraChange(CameraPosition position) { 
       if(position.zoom != oldZoom) { 
        try { 

         clusters = null;       
         map.clear(); 
         Clusterizer.setContext(getApplicationContext()); 
         clusters = Clusterizer.clusterMarkers(map, markers, INTERVAL); 

        } catch (ExecutionException e) { 
         e.printStackTrace(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       }    
       oldZoom = position.zoom; 
      } 
     }); 

    } 


    private void loadMarkers() { 

     markers.add(new Marker(41.40520680710229,2.191342011603823,"Glorias1","Centro Comercial",true).getMarker()); 
     markers.add(new Marker(41.40520680710229+0.0005,2.191342011603823-0.0005,"Glorias11","Centro Comercial",true).getMarker()); 
     markers.add(new Marker(41.40520680710229+0.0005,2.191342011603823+0.0005,"Glorias111","Centro Comercial",true).getMarker()); 
     markers.add(new Marker(41.40520680710229-0.0005,2.191342011603823+0.0005,"Glorias1111","Centro Comercial",true).getMarker()); 
     markers.add(new Marker(41.40520680710229-0.0005,2.191342011603823-0.0005,"Glorias1111","Centro Comercial",true).getMarker()); 
    } 

而且,在Asyntask後計算集羣(它確定)的postExecute方法,做到這一點:

@Override 
     protected void onPostExecute(
       LinkedHashMap<Point, ArrayList<MarkerOptions>> clusters) { 

      map.clear(); 
      for(Point point: clusters.keySet()) { 
       ArrayList<MarkerOptions> markersForPoint = clusters.get(point); 
       MarkerOptions mainMaker= markersForPoint.get(0); 
       //If the point (taken from cluster, has more than 1 markerOption, means that has been clusterized, so It have to be printed with the //modified canvas that contains the number of markers clusterized) 
       if(markersForPoint.size() > 1) { 
        mainMaker.title(Integer.toString(markersForPoint.size())); 

        Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
        Bitmap bmp = Bitmap.createBitmap(80, 80, conf); 
        Canvas canvas1 = new Canvas(bmp); 
        Paint color = new Paint(); 
        color.setTextSize(35); 
        color.setColor(Color.BLACK 
        canvas1.drawBitmap(BitmapFactory.decodeResource(context.getResources(), 
          R.drawable.pin), 0,0, color);     
        canvas1.drawText("",10,40,color); 
        canvas1.drawText(Integer.toString(markersForPoint.size()), 10, 40, color); 

        mainMaker.icon(BitmapDescriptorFactory.fromBitmap(bmp)); 
        mainMaker.anchor(0.5f, 1); 
       } 
       map.addMarker(mainMaker); 
      } 
     } 

而這裏的問題...

如果它的集羣有沒問題,它表明沒問題,但是當我放大cluster list這沒關係,但標記不是

這就是看到羣集何時「聚集」,何時被隱藏以不聚集

我該如何解決它?我在添加標記前清潔地圖...

謝謝大家!

集羣:

enter image description here

未加入羣集 enter image description here enter image description here

+0

目前尚不清楚你要求什麼。 – 2013-05-09 23:28:59

+0

@MaciejGórski我嘗試用其他方式解釋.. ^^。當我在地圖上放大或縮小時,如果要關閉標記(在屏幕上以像素爲單位),我會將要關閉的那個(例如70像素)聚簇。當標記聚集時,我不使用默認的GMap標記,我使用了一個自定義標記,用代表的標記數「繪製」。但是當我放大時(集羣消失,現在可以看到5個標記(例如),表示集羣的標記仍然是自定義的,當應該是默認標記時 – Shudy 2013-05-10 05:25:31

回答

0

嘗試增加:

 color.setTextAlign(Align.CENTER); 

您可能需要調整油漆的原點以使其居中。

0

我前清理地圖添加標記...

我看不到結算代碼,它好像你不叫上老的Marker.remove()。很明顯,你有一個算法問題,如果你不提供完整的代碼,它將無法回答。

您可以嘗試使用Google Maps Android API v2的開源集羣算法之一:Android Maps ExtensionsClusterkraf。後者似乎更接近你想要達到的目標 - 這是基於距離的。

+0

我已經添加了更多的編碼我使用'map.clear()'從標記中清除映射,並且在AsynTask的postExecute()中,如果標記是集羣化的,我就可以「繪製」自定義標記 – Shudy 2013-05-13 12:55:12