android – DefaultClusterRenderer getMarker()在缩放时返回null

android – DefaultClusterRenderer getMarker()在缩放时返回null,第1张

概述我想在点击时更改群集标记的背景.我通过这样做 @OverrideonClusterClick(Cluster<MyObject> cluster) { Marker marker = renderer.getMarker(cluster); marker.setIcon(....);} 对于一种情况,这很好用:当我放大或缩小时,簇标记的数量不会改变.例如,如果我有一个15集群和 我想在点击时更改群集标记的背景.我通过这样做
@OverrIDeonClusterClick(Cluster<MyObject> cluster) {    Marker marker = renderer.getMarker(cluster);    marker.setIcon(....);}

对于一种情况,这很好用:当我放大或缩小时,簇标记的数量不会改变.例如,如果我有一个15集群和一个5集群,然后放大或缩小一个级别,保留相同的两个集群.现在单击其中一个renderer.getMarker(cluster)将返回null.如果它们在缩放后重新聚类,则getMarker不为null.

我的DefaultClusterRenderer在下面.我检查了onClusteredRendered上的标记,它永远不会为空.这是DefaultClusterRenderer中的错误还是我应该做些什么?

private class MyRenderer extends DefaultClusterRenderer<MyObject> {    private IconGenerator iconGenerator;    private float density;    public MyRenderer(Context context,GoogleMap map,ClusterManager<MyObject> clusterManager) {        super(context,map,clusterManager);        density = context.getResources().getdisplayMetrics().density;    }    @OverrIDe    protected voID onBeforeClusterItemRendered(MyObject item,MarkerOptions markerOptions) {        markerOptions.icon(BitmapDescriptorFactory.fromresource(R.drawable.my_pin));    }    @OverrIDe    protected voID onBeforeClusterRendered(Cluster<MyObject> cluster,MarkerOptions markerOptions) {        if(iconGenerator == null) {            iconGenerator = new IconGenerator(getActivity());            iconGenerator.setContentVIEw(makeTextVIEw(getActivity()));        }        iconGenerator.setBackground(makeBackground(false));        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(iconGenerator.makeIcon(String.valueOf(cluster.getSize()))));    }    @OverrIDe    protected voID onClusterRendered(Cluster<MyObject> cluster,Marker marker) {        super.onClusterRendered(cluster,marker);        // Marker is never null here    }    @OverrIDe    protected boolean shouldRenderAsCluster(Cluster<MyObject> cluster) {        return cluster.getSize() > 1;    }    private ShapeDrawable makeBackground(boolean isClicked) {        ShapeDrawable background = new ShapeDrawable(new ovalShape());        background.setcolorFilter(ContextCompat.getcolor(getActivity(),isClicked ? R.color.cluster_marker_clicked : R.color.cluster_marker_unclicked),PorterDuff.Mode.SRC_Atop);        return background;    }    private SquareTextVIEw makeTextVIEw(Context context) {        SquareTextVIEw squareTextVIEw = new SquareTextVIEw(context);        VIEwGroup.LayoutParams layoutParams = new VIEwGroup.LayoutParams(-2,-2);        squareTextVIEw.setLayoutParams(layoutParams);        squareTextVIEw.setTextcolor(ContextCompat.getcolor(getActivity(),R.color.white));        squareTextVIEw.setTypeface(Utils.getFontBold(getActivity()));        squareTextVIEw.setID(com.Google.maps.androID.R.ID.text);        int twelveDpi = (int) (12.0F * density);        squareTextVIEw.setpadding(twelveDpi,twelveDpi,twelveDpi);        return squareTextVIEw;    }    public IconGenerator getIconGenerator(boolean isClicked) {        iconGenerator.setBackground(makeBackground(isClicked));        return iconGenerator;    }}

初始化ClusterManager:

final ClusterManager<MyObject> mClusterManager = new ClusterManager<>(getActivity(),GoogleMap);    mClusterManager.addItems(items);    renderer = new Customrenderer(getActivity(),GoogleMap,mClusterManager);    mClusterManager.setRenderer(renderer);    mClusterManager.cluster();    mClusterManager.setonClusterItemClickListener(this);    GoogleMap.setonMarkerClickListener(mClusterManager);

@antonio:这个初始化对我有用:

public class MapsActivity extends FragmentActivity        implements ClusterManager.OnClusterClickListener<MyObject> {    // ...    private voID setUpClusterer() {        GoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186,-0.126446),10));        mClusterManager = new ClusterManager<MyObject>(this,GoogleMap);        mClusterManager.setonClusterClickListener(this);        renderer = new MyRenderer(this,mClusterManager);        mClusterManager.setRenderer(renderer);        GoogleMap.setonCamerachangelistener(mClusterManager);        GoogleMap.setonMarkerClickListener(mClusterManager);        addItems();    }    private voID addItems() {        // Set some lat/lng coordinates to start with.        double lat = 51.5145160;        double lng = -0.1270060;        // Add ten cluster items in close proximity,for purposes of this example.        for (int i = 0; i < 10; i++) {            double offset = i / 60d;            lat = lat + offset;            lng = lng + offset;            MyObject offsetItem = new MyObject(lat,lng);            mClusterManager.addItem(offsetItem);        }    }    @OverrIDe    public boolean onClusterClick(final Cluster<MyObject> cluster) {        Marker marker = renderer.getMarker(cluster);        marker.setIcon(BitmapDescriptorFactory.fromresource(R.drawable.my_newpin));        return true;    }}
解决方法 在上面的情况下,无法让renderer.getMarker(cluster)返回一个标记.解决方法是创建:
Map <Cluster<MyObject>,Marker> clusterMarkerMap = new HashMap<>();

然后在DefaultClusterRenderer回调中添加它们,因为标记在那里永远不为null:

@OverrIDeprotected voID onClusterRendered(Cluster<MyObject> cluster,Marker marker) {    super.onClusterRendered(cluster,marker);    clusterMarkerMap.put(cluster,marker);}

因为DefaultClusterManager在摄像机位置更改时清除它们,所以在创建新的标记映射之前擦除它们:

GoogleMap.setonCamerachangelistener(new GoogleMap.OnCamerachangelistener() {        @OverrIDe        public voID onCameraChange(Cameraposition cameraposition) {            // Clear the map here because the markers will be recreated            // when the manager is notifIEd of a (zoom level) camera change            if(zoomLevelChanged)                 clusterMarkerMap.clear();            mClusterManager.onCameraChange(cameraposition);        }    });

现在我可以成功获得标记clusterMarkerMap.get(cluster)

总结

以上是内存溢出为你收集整理的android – DefaultClusterRenderer getMarker()在缩放时返回null全部内容,希望文章能够帮你解决android – DefaultClusterRenderer getMarker()在缩放时返回null所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1138818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存