将类别放到标记android map api v2

时间:2014-06-30 03:23:35

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

我想将我的标记分类为http://econym.org.uk/gmap/example_categories.htm,我不知道如何将类型放入我的标记。我卡在这里。这是我的代码。我试过这个,但似乎错了

Marker malls = mMap.addMarker(new MarkerOptions()
    .position(new LatLng(14.589740, 120.982111))
    .title("Welcome to the City of Maynila"))
    .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.blue));

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

您拥有的最佳方法是使用群集管理器将项目添加到地图中。

您创建一个实现ClusterItem的类并创建您自己的项目。 您需要有一个函数来选择名为setIconByData的图标类型:

public class MyItem implements ClusterItem {
    private final LatLng mPosition;
    private MarkerOptions markerOptions;
    private String data;
    private int iconReference;

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

public MyItem(MarkerOptions markerOptions) {
    this.setMarkerOptions(markerOptions);
    mPosition = markerOptions.getPosition();
}

public MyItem(LatLng latLong) {
    mPosition = latLong;
}

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

public MarkerOptions getMarkerOptions() {
    return markerOptions;
}

public void setMarkerOptions(MarkerOptions markerOptions) {
    this.markerOptions = markerOptions;
}

public int getIconReference() {
    return iconReference;
}

public void setIconReference(int iconReference) {
    this.iconReference = iconReference;
}

public void setIconByData(String data, MyItem myItem) {
    if(data.equals("museum"))
        myItem.setIconReference(iconMuseum);
    else
        myItem.setIconReference(iconGolf);
}

}

您需要在活动或片段类中使用函数来组织项目。 此函数需要返回项目集合:

private Collection<MyItem> organizeItems(LatLng latlong) {
   Collection<MyItem> res = new ArrayList<MyItem>();
   MarkerOptions markerOptions = new MarkerOptions().position(latlong);
   MyItem item = new MyItem(markerOptions);
   item.setIconByValencia("golf", item);
   res.add(item);   
   return res;
}

然后,当您想要清理经理并添加新项目时:

mClusterManager = new ClusterManager<MyItem>(v.getContext(), map); //on create

//when you want to refresh the items:
mClusterManager.clearItems();
mClusterManager.addItems(addItems(BOUNDS, TicketTypeProduct.TO));
mClusterManager.cluster();

您可以在此处查看使用群集和群集管理器的详细信息: https://developers.google.com/maps/documentation/android/utility/marker-clustering?hl=en-EN