【问题标题】:Adding multiple custom markers and cluster them添加多个自定义标记并将它们聚类
【发布时间】:2015-05-23 06:18:30
【问题描述】:

我有这个活动:

public class MapActivity extends FragmentActivity {
private GoogleMap mMap;
private ClusterManager<GasStationItem> mClusterManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_gas_station);

    try {
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            mMap.setMyLocationEnabled(true);

            setUpClusters();
        }
    } catch (NullPointerException e){
        e.printStackTrace();
    }


}

private void setUpClusters(){
    mClusterManager = new ClusterManager<GasStationItem>(this, mMap);

    mMap.setOnCameraChangeListener(mClusterManager);
    mMap.setOnMarkerClickListener(mClusterManager);

    mClusterManager.setRenderer(new OwnIconRendered(this ,mMap ,mClusterManager));

    addItems();
}

private void addItems(){

    ParseQuery<GasStationItem> query = ParseQuery.getQuery(GasStationItem.class);
    query.setLimit(100);
    query.findInBackground(new FindCallback<GasStationItem>() {
        @Override
        public void done(List<GasStationItem> gasStationItems, ParseException e) {
            if (e == null) {

                for (int i=0; i< gasStationItems.size(); i++){
                    GasStationItem gasStationItem = gasStationItems.get(i);
                    mClusterManager.addItem(gasStationItem);
                }

            } else {
                Log.d("ERROR:", "" + e.getMessage());
            }
        }
    });

}

class OwnIconRendered extends DefaultClusterRenderer<GasStationItem> {

    private BitmapDescriptor mBitmapMarker;

    public OwnIconRendered(Context context, GoogleMap map,
                           ClusterManager<GasStationItem> clusterManager) {
        super(context, map, clusterManager);
    }

    @Override
    protected void onBeforeClusterItemRendered(GasStationItem item, MarkerOptions markerOptions) {

        mBitmapMarker = customizeMarkers(item.getName());
        markerOptions.icon(mBitmapMarker);
        markerOptions.title(item.getName());
        super.onBeforeClusterItemRendered(item, markerOptions);
    }
}


private BitmapDescriptor customizeMarkers(String input){

    BitmapDescriptor bitmapMarker = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher);
    if(input.contains("Gazprom")){
        bitmapMarker = BitmapDescriptorFactory.fromResource(R.mipmap.gazprom);
    } else {
        if(input.contains("Mol")) {
            bitmapMarker = BitmapDescriptorFactory.fromResource(R.mipmap.mol);
        } else {
            if(input.contains("Petrom")){
                bitmapMarker = BitmapDescriptorFactory.fromResource(R.mipmap.petrom);
            } else {
                if(input.contains("OMV")){
                    bitmapMarker = BitmapDescriptorFactory.fromResource(R.mipmap.omv);
                } else {
                    if(input.contains("Lukoil")){
                        bitmapMarker = BitmapDescriptorFactory.fromResource(R.mipmap.lukoil);
                    } else {
                        if(input.contains("Rompetrol")){
                            bitmapMarker = BitmapDescriptorFactory.fromResource(R.mipmap.rompetrol);
                        }
                    }
                }
            }
        }
    }

    return bitmapMarker;
}
}

LatLng 用于标记我从数据库中读取它,我为此使用解析。我还覆盖 Renderer 来自定义我的标记。

当我启动活动时,甚至没有绘制地图。我认为是相对的来自 logcat 的单个错误是:

03-20 00:31:51.425 1996-1996/com.driverbuddy.costeiu.driverbuddy I/Choreographer:跳过33帧!该应用程序可能也在做 在它的主线程上做了很多工作。

【问题讨论】:

    标签: android google-maps google-maps-markers markerclusterer


    【解决方案1】:

    这可能是因为您在主线程中执行繁重的操作。

    Android 是单线程系统,只有一个主线程,所有其他子系统都在其中挂钩以执行活动、服务、内容提供者等任务。

    您可能希望将 setUpClusters() 移至 AsyncTask

    【讨论】:

    • 我创建了新的类,它扩展了 AsynkTask,我只移动了 setUpClusters() 调用方法并在 onCreate() 中执行:new class.execute();什么也没发生
    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多