Android:集群标记计数未使用自定义标记正确显示

Posted

技术标签:

【中文标题】Android:集群标记计数未使用自定义标记正确显示【英文标题】:Android : Cluster marker count not showing properly with custom marker 【发布时间】:2019-12-10 17:06:36 【问题描述】:

我正在使用Google Maps android Marker Clustering Utility 在地图上绘制多个标记,但我的问题是群集标记计数没有显示在使用自定义标记的正确位置。我添加了图片以进行更多说明。

下面是loadmap()

private void LoadMap() 
        try 
            mapFragment.getMapAsync(new OnMapReadyCallback() 
                @Override
                public void onMapReady(GoogleMap mMap) 
                    gMap = mMap;
                    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    mMap.clear();
                    mClusterManager = new ClusterManager<>(getActivity(), mMap);
                    //mMap.getUiSettings().setZoomControlsEnabled(true);
                    mMap.setOnCameraIdleListener(mClusterManager);
                    mMap.setOnMarkerClickListener(mClusterManager);
                    mClusterManager.setRenderer(new MarkerClusterRenderer(getActivity(), mMap, mClusterManager));

                    if(arrayListLatLong.size() > 0) 
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(arrayListLatLong.get(0).getLatitude(), arrayListLatLong.get(0).getLongitude()), 10));
                        for (int i = 0; i < arrayListLatLong.size(); i++) 
                            ClusterMapItems offsetItem = new ClusterMapItems(arrayListLatLong.get(i).getLatitude(), arrayListLatLong.get(i).getLongitude(), arrayListLatLong.get(i).getVehicleStatus(),
                                    arrayListLatLong.get(i).getRegistrationId(), arrayListLatLong.get(i).getCurrentVehicleSpeed(), arrayListLatLong.get(i).getVehicleUpdateTimeStr(),
                                    arrayListLatLong.get(i).getGroupName(), arrayListLatLong.get(i).getVin(), arrayListLatLong.get(i).getDescription(), arrayListLatLong.get(i).getVehicleVariant(),
                                    arrayListLatLong.get(i).getAdblueLevel(), arrayListLatLong.get(i).getTotalFuelConsumption(), arrayListLatLong.get(i).getFuelTankLevel());
                            mClusterManager.addItem(offsetItem);
                            mClusterManager.cluster();

                            mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<ClusterMapItems>() 
                                @Override
                                public boolean onClusterItemClick(ClusterMapItems clusterMapItems) 
                                    MyFleetBottomSheetFragment bottomSheetFragment = new MyFleetBottomSheetFragment();
                                    Bundle bundle= new Bundle();
                                    bundle.putParcelable("clusterMapItems", clusterMapItems);
                                    bottomSheetFragment.setArguments(bundle);
                                    bottomSheetFragment.show(getActivity().getSupportFragmentManager(), bottomSheetFragment.getTag());
                                    return false;
                                
                            );
                        
                    
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(20.5937, 78.9629), 0));
                
            );
         catch (IndexOutOfBoundsException e)
            e.printStackTrace();
         catch (Exception e)
            e.printStackTrace();
        
    

MarkerClusterRenderer.Class

public class MarkerClusterRenderer extends DefaultClusterRenderer<ClusterMapItems> 


    private final IconGenerator iconGenerator;
    private Context contextN;

    public MarkerClusterRenderer(Context context, GoogleMap map, ClusterManager<ClusterMapItems> clusterManager ) 
        super(context, map, clusterManager);
        iconGenerator = new IconGenerator(context);
        contextN = context;
    

    @Override
    protected int getBucket(Cluster<ClusterMapItems> cluster) 
        return cluster.getSize();
    

    @Override
    protected void onClusterItemRendered(ClusterMapItems clusterItem, Marker marker) 
        super.onClusterItemRendered(clusterItem, marker);
    

    @Override
    protected void onBeforeClusterItemRendered(ClusterMapItems item, MarkerOptions markerOptions) 
            if(item.getVehicleStatus().equals("NEW"))
                BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ic_green_pointer);
                markerOptions.icon(markerDescriptor);
            

            if(item.getVehicleStatus().equals("RUNNING"))
                BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ic_black_pointer);
                markerOptions.icon(markerDescriptor);
            
    

    @Override
    protected void onBeforeClusterRendered(Cluster<ClusterMapItems> cluster, MarkerOptions markerOptions) 
        final Drawable clusterIcon = contextN.getResources().getDrawable(R.drawable.ic_cluster_purple);
        iconGenerator.setBackground(clusterIcon);
        iconGenerator.setTextAppearance(com.google.maps.android.R.style.amu_ClusterIcon_TextAppearance);
        Bitmap icon = iconGenerator.makeIcon(String.valueOf(cluster.getSize()));
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));

    

请帮助我如何在集群标记的中心显示集群计数?有没有可能?

我已经尝试过这个给定的 SO answer 我仍然面临同样的问题。

【问题讨论】:

结帐this How to center text on android IconGenerator的可能重复 @MaratZangiev 我已经尝试过相同的答案,但我提出的问题对我没有用。 您需要为集群指定自定义View 而不是Drawable,它会正常工作 从答案中尝试 【参考方案1】:

为您的集群创建自定义 cluster_view.xml 布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true">

    <TextView
        android:layout_
        android:layout_
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Text"
        android:textColor="#000000"
        android:id="@+id/amu_text"
        android:gravity="center" />
</FrameLayout>

您应该在内部指定一个TextView,id 为amu_text

之后更改您的Renderer 代码如下:

public class MarkerClusterRenderer extends DefaultClusterRenderer<ClusterMapItems> 


    private final IconGenerator iconGenerator;
    private Context contextN;

    public MarkerClusterRenderer(Context context, GoogleMap map, ClusterManager<ClusterMapItems> clusterManager ) 
        super(context, map, clusterManager);
        iconGenerator = new IconGenerator(context);
        contextN = context;
    

    @Override
    protected int getBucket(Cluster<ClusterMapItems> cluster) 
        return cluster.getSize();
    

    @Override
    protected void onClusterItemRendered(ClusterMapItems clusterItem, Marker marker) 
        super.onClusterItemRendered(clusterItem, marker);
    

    @Override
    protected void onBeforeClusterItemRendered(ClusterMapItems item, MarkerOptions markerOptions) 
            if(item.getVehicleStatus().equals("NEW"))
                BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ic_green_pointer);
                markerOptions.icon(markerDescriptor);
            

            if(item.getVehicleStatus().equals("RUNNING"))
                BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ic_black_pointer);
                markerOptions.icon(markerDescriptor);
            
    

    @Override
    protected void onBeforeClusterRendered(Cluster<ClusterMapItems> cluster, MarkerOptions markerOptions) 
        final Drawable clusterIcon = contextN.getResources().getDrawable(R.drawable.ic_cluster_purple);
        iconGenerator.setBackground(clusterIcon);
        LayoutInflater myInflater = (LayoutInflater)contextN.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View clusterView= myInflater.inflate(R.layout.cluster_view, null, false);
        iconGenerator.setContentView(clusterView);
        iconGenerator.makeIcon(String.valueOf(cluster.getSize()));
        BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(iconGenerator.makeIcon());
        markerOptions.icon(icon);

    

【讨论】:

谢谢@Marat Zangiev 根据您的回答和建议的链接,我已经尝试过仍然存在问题,您可以查看此链接imgur.com/a/trT7VhD 我对此尝试了许多变体,似乎实现它的唯一方法是设置具有与标准群集图标大小相似大小的背景。结帐更多there

以上是关于Android:集群标记计数未使用自定义标记正确显示的主要内容,如果未能解决你的问题,请参考以下文章

谷歌在 Android 上使用集群映射自定义标记图标

React Native Maps - 自定义标记图像未在更高的 Android 版本中呈现

android上未显示自定义标记图标[关闭]

在谷歌地图自定义标记未显示在三星 S8 或 Android 的相同版本 7.0

Android 获取未聚集的标记

带有毕加索的谷歌地图集群项目标记图标