【问题标题】:Add Title to Marker in Google Map Cluster在谷歌地图集群中为标记添加标题
【发布时间】:2014-09-07 14:02:06
【问题描述】:

我正在创建一个包含数百个标记的应用,因此我决定实施集群是一个好主意。但是,我遇到了为集群中的标记添加标题的问题。当我创建标记的信息窗口时,我需要这些数据稍后从 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();
        }
    }

感谢您的帮助:)

【问题讨论】:

  • 嗨,我关注了你的问题和答案,但是当我点击集群项目时,我一直无法显示实际的信息窗口。有什么建议吗?

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


【解决方案1】:

为您提供了一个全局解决方案,可帮助您添加标题、sn-p 和图标,以便您获得所需的内容。

修改您的 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;

}

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

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);
    }
}

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

 mClusterManager.setRenderer(new OwnRendring(getApplicationContext(),mMap,mClusterManager));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-24
    • 2013-01-03
    • 1970-01-01
    • 2016-04-03
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多