gpt4 book ai didi

Android maps utils cluster 图标颜色

转载 作者:IT王子 更新时间:2023-10-28 23:33:28 27 4
gpt4 key购买 nike

有什么方法可以改变集群项的背景颜色吗? (显示标记计数的那个,例如 100+、200+ ...)。我试图查看 ClusterManager 的源代码,但找不到任何更改颜色的选项,但也许这里有人知道如何做到这一点。我基本上想把这些颜色“具体化”一下。

最佳答案

我能够通过使用 this demo 得到一个粗略的实现。以图书馆样本为指导。

我使用了来自 here 的 Material Design Icons 中的 lens 图标.下载 lens 压缩包后,我将 ic_lens_black_24dp.png 放在 drawable 文件夹下。然后我使用了Drawable.setColorFilter()方法来改变代码中的默认颜色。

我还能够更改默认标记颜色,并认为我也会在此处包含它。

首先,通过调用setRenderer()设置一个Renderer:

 mClusterManager.setRenderer(new MyClusterRenderer(this, mMap,
mClusterManager));

然后,定义 MyClusterRenderer 类:

public class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {

private final IconGenerator mClusterIconGenerator = new IconGenerator(getApplicationContext());

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

@Override
protected void onBeforeClusterItemRendered(MyItem item,
MarkerOptions markerOptions) {

BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA);

markerOptions.icon(markerDescriptor);
}

@Override
protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
super.onClusterItemRendered(clusterItem, marker);
}

@Override
protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions){

final Drawable clusterIcon = getResources().getDrawable(R.drawable.ic_lens_black_24dp);
clusterIcon.setColorFilter(getResources().getColor(android.R.color.holo_orange_light), PorterDuff.Mode.SRC_ATOP);

mClusterIconGenerator.setBackground(clusterIcon);

//modify padding for one or two digit numbers
if (cluster.getSize() < 10) {
mClusterIconGenerator.setContentPadding(40, 20, 0, 0);
}
else {
mClusterIconGenerator.setContentPadding(30, 20, 0, 0);
}

Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
}

全类代码:

public class MapsActivity extends AppCompatActivity
implements ClusterManager.OnClusterItemInfoWindowClickListener<MyItem> {

private ClusterManager<MyItem> mClusterManager;
private MyItem clickedClusterItem;
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_maps);

setUpMapIfNeeded();
}

@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}


private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();

// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}

}
}

private void setUpMap() {

mMap.getUiSettings().setMapToolbarEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

mClusterManager = new ClusterManager<>(this, mMap);

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.779977,-122.413742), 10));

mMap.setOnCameraChangeListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);

mClusterManager.setRenderer(new MyClusterRenderer(this, mMap,
mClusterManager));

mMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());

mMap.setOnInfoWindowClickListener(mClusterManager); //added
mClusterManager.setOnClusterItemInfoWindowClickListener(this); //added

mClusterManager
.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
@Override
public boolean onClusterItemClick(MyItem item) {
clickedClusterItem = item;
return false;
}
});



addItems();

mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(
new MyCustomAdapterForItems());

}

private void addItems() {

double latitude = 37.779977;
double longitude = -122.413742;
for (int i = 0; i < 10; i++) {
double offset = i / 60d;

double lat = latitude + offset;
double lng = longitude + offset;
MyItem offsetItem = new MyItem(lat, lng, "title " + i+1, "snippet " + i+1);
mClusterManager.addItem(offsetItem);

}

}

//added with edit
@Override
public void onClusterItemInfoWindowClick(MyItem myItem) {

//Cluster item InfoWindow clicked, set title as action
Intent i = new Intent(this, OtherActivity.class);
i.setAction(myItem.getTitle());
startActivity(i);

//You may want to do different things for each InfoWindow:
if (myItem.getTitle().equals("some title")){

//do something specific to this InfoWindow....

}

}

public class MyCustomAdapterForItems implements GoogleMap.InfoWindowAdapter {

private final View myContentsView;

MyCustomAdapterForItems() {
myContentsView = getLayoutInflater().inflate(
R.layout.info_window, null);
}
@Override
public View getInfoWindow(Marker marker) {

TextView tvTitle = ((TextView) myContentsView
.findViewById(R.id.txtTitle));
TextView tvSnippet = ((TextView) myContentsView
.findViewById(R.id.txtSnippet));

tvTitle.setText(clickedClusterItem.getTitle());
tvSnippet.setText(clickedClusterItem.getSnippet());

return myContentsView;
}

@Override
public View getInfoContents(Marker marker) {
return null;
}
}

public class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {

private final IconGenerator mClusterIconGenerator = new IconGenerator(getApplicationContext());

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

@Override
protected void onBeforeClusterItemRendered(MyItem item,
MarkerOptions markerOptions) {

BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA);

markerOptions.icon(markerDescriptor);
}

@Override
protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
super.onClusterItemRendered(clusterItem, marker);
}

@Override
protected void onBeforeClusterRendered(Cluster<MyItem> cluster, MarkerOptions markerOptions){

final Drawable clusterIcon = getResources().getDrawable(R.drawable.ic_lens_black_24dp);
clusterIcon.setColorFilter(getResources().getColor(android.R.color.holo_orange_light), PorterDuff.Mode.SRC_ATOP);

mClusterIconGenerator.setBackground(clusterIcon);

//modify padding for one or two digit numbers
if (cluster.getSize() < 10) {
mClusterIconGenerator.setContentPadding(40, 20, 0, 0);
}
else {
mClusterIconGenerator.setContentPadding(30, 20, 0, 0);
}

Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
}
}

结果:

初始应用启动:

No clustering

缩小,一些聚类:

initial clustering

再次缩小,所有标记都聚集在一起:

all Markers clustered

关于Android maps utils cluster 图标颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30967961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com