添加多个自定义标记并将其聚类

时间:2015-03-19 22:45:17

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

我有这个活动:

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我从数据库中读取它,我使用解析。我也覆盖了渲染器以自定义我的标记。

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

  

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

1 个答案:

答案 0 :(得分:0)

这可能发生了,因为你在主线程中执行繁重的操作。

Android是单线程系统,所有其他子系统都有单个主线程来执行任务,如活动,服务,内容提供商等。

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