添加标题为标记在谷歌地图集群 [英] Add Title to Marker in Google Map Cluster
问题描述
我创建一个应用程序与数百个标记,所以我决定实施集群是一个好主意。不过,我碰到添加标题到集群中的标记的问题。我需要这个数据,当我创建标记的信息窗口以后检索JSON项目。所以,总结一下我的问题是,如何将添加一个字符串作为标题到每个标记在一个集群中。
I'm creating an app with hundreds of markers so I decided implementing clustering was a good idea. However, I've run into the problem of adding a title to the markers in the cluster. I need this data to later retrieve items from JSON when I create the marker's info window. So to sum up my question is, how would I add a String as a title to each Marker in a cluster.
我目前的code:
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();
}
}
感谢任何帮助:)
Thanks for any help :)
推荐答案
有对你是一个全球性的解决方案,帮助添加标题,摘要和图标,这样你可以得到你想要的东西。
there is a global solution for you that help to add title, snippet and icon so you can get what you want.
修改您的ClusterItem对象,并添加3个变量:
Modify your ClusterItem Object and add 3 variables :
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;
}
和创建后,你的服装渲染:
And after you create your costume render :
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()函数前:
After that just put this line in your SetUpCluster() function before addItems():
mClusterManager.setRenderer(new OwnRendring(getApplicationContext(),mMap,mClusterManager));
这篇关于添加标题为标记在谷歌地图集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!