在 Google Map Cluster 中为标记添加标题

Eva*_*van 1 android google-maps google-maps-markers

我正在创建一个包含数百个标记的应用程序,因此我决定实施聚类是一个好主意。但是,我遇到了为集群中的标记添加标题的问题。当我创建标记的信息窗口时,我需要这些数据以便稍后从 JSON 中检索项目。所以总结我的问题是,我如何将一个字符串作为标题添加到集群中的每个标记。

我目前的代码:

public class MyItem implements ClusterItem {
    private final LatLng mPosition;

    public MyItem(double lat, double lng) {
        mPosition = new LatLng(lat, lng);
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }
}

for (int i = 0; i < activity.m_jArry.length(); i++)
    {
        JSONObject j;
        try {
            j = activity.m_jArry.getJSONObject(i);
            mClusterManager.addItem(new MyItem(j.getDouble("lat"), j.getDouble("lon")));
            //mMap.addMarker(new MarkerOptions().title(j.getString("Unique")).snippet(i + "").position(new LatLng(j.getDouble("lat"), j.getDouble("lon"))));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助 :)

小智 5

有一个全局解决方案可以帮助您添加标题、片段和图标,以便您获得所需的内容。

修改您的 ClusterItem 对象并添加 3 个变量:

public class MyItem implements ClusterItem {

private final LatLng mPosition;
BitmapDescriptor icon;
String title;
String snippet;

public MyItem(BitmapDescriptor ic,Double lat , Double lng,String tit ,String sni)
{
    mPosition = new LatLng(lat,lng);
    icon = ic;
    title = tit;
    snippet = sni;

}
Run Code Online (Sandbox Code Playgroud)

在你创建你的服装渲染之后:

public class OwnRendring extends DefaultClusterRenderer<MyItem> {

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


    protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {

        markerOptions.icon(item.getIcon());
        markerOptions.snippet(item.getSnippet());
        markerOptions.title(item.getTitle());
        super.onBeforeClusterItemRendered(item, markerOptions);
    }
}
Run Code Online (Sandbox Code Playgroud)

之后,只需将此行放在您的 SetUpCluster() 函数中 addItems() 之前:

 mClusterManager.setRenderer(new OwnRendring(getApplicationContext(),mMap,mClusterManager));
Run Code Online (Sandbox Code Playgroud)