[英]Show and update icon of marker in java android google map
I use the code from github(It's not my code) as an example https://github.com/mitchtabian/Google-Maps-2018/tree/displaying-trip-duration-of-polylines-end 我使用github中的代码(不是我的代码)作为示例https://github.com/mitchtabian/Google-Maps-2018/tree/displaying-trip-duration-of-polylines-end
This is my issue: 这是我的问题:
I have two problems: 我有两个问题:
onBeforeClusterItemRendered()
method doesn't show the image. onBeforeClusterItemRendered()
方法不显示图像。 If I use the image from the resource as in the original example then it works. The setUpdateMarker()
method changes two images only on one marker instead of showing the image on each marker. setUpdateMarker()
方法仅在一个标记上更改两个图像,而不是在每个标记上显示图像。
@Override protected void onBeforeClusterItemRendered(ClusterMarker item, MarkerOptions markerOptions) { super.onBeforeClusterItemRendered(item, markerOptions); Bitmap mIcon = null ; try { InputStream in = new URL(item.getIconPath()).openStream(); mIcon = BitmapFactory.decodeStream(in); imageView.setImageBitmap(mIcon); Bitmap icon = iconGenerator.makeIcon(); markerOptions.icon( BitmapDescriptorFactory.fromBitmap(icon)).title(item.getTitle()); } catch (Exception e) { e.printStackTrace(); } } public void setUpdateMarker(ClusterMarker clusterMarker) { marker = getMarker(clusterMarker); if (marker != null) { marker.setPosition(clusterMarker.getPosition()); new DownloadImageTask().execute(clusterMarker.getIconPath()); } } private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { @Override protected Bitmap doInBackground(String... urls) { Bitmap mIcon = null; try { InputStream in = new URL(urls[0]).openStream(); mIcon = BitmapFactory.decodeStream(in); } catch (Exception e) { e.printStackTrace(); } return mIcon; } @Override protected void onPostExecute(Bitmap result) { if (marker!=null && result != null) { Bitmap resizedBitmap = Bitmap.createScaledBitmap(result, markerWidth, markerHeight, false); marker.setIcon( BitmapDescriptorFactory.fromBitmap(resizedBitmap)); } } }
Updated: I show the constructor of this class too: 更新:我也显示了此类的构造函数:
public class MyClusterManagerRenderer extends DefaultClusterRenderer<ClusterMarker> {
private static final String TAG = " ClusterManagerRenderer";
private final IconGenerator iconGenerator;
private final ImageView imageView;
private final int markerWidth;
private final int markerHeight;
private final int padding;
private Marker marker;
public MyClusterManagerRenderer(Context context, GoogleMap googleMap,
ClusterManager<ClusterMarker> clusterManager) {
super(context, googleMap, clusterManager);
// initialize cluster item icon generator
iconGenerator = new IconGenerator(context.getApplicationContext());
imageView = new ImageView(context.getApplicationContext());
markerWidth = (int) context.getResources().getDimension(R.dimen.custom_marker_image);
markerHeight = (int) context.getResources().getDimension(R.dimen.custom_marker_image);
imageView.setLayoutParams(new ViewGroup.LayoutParams(markerWidth, markerHeight));
padding = (int) context.getResources().getDimension(R.dimen.custom_marker_padding);
imageView.setPadding(padding, padding, padding, padding);
iconGenerator.setContentView(imageView);
}
You can use your method in onClusterItemRendered() method. 您可以在onClusterItemRendered()方法中使用您的方法。 Take a look this answer https://stackoverflow.com/a/34029984/7917629
看看这个答案https://stackoverflow.com/a/34029984/7917629
@Override
protected void onClusterItemRendered(ClusterMarker item, final Marker marker) {
String imageUrl = item.getImageUri();
// here you can call your background task
// or some other option like Glide
// you put your icon as:
// marker.setIcon(BitmapDescriptorFactory.fromBitmap(resizedBitmap));
}
------ Update how to use glide (my version 4.7.1) here ------在此处更新如何使用滑行 (我的版本4.7.1)
@Override
protected void onClusterItemRendered(EventMarker clusterItem, final Marker marker) {
String imageUrl = clusterItem.getImageUri();
if (imageView != null && imageUrl != null && !imageUrl.isEmpty()) {
Glide.with(mContext.getApplicationContext())
.load(imageUrl)
.apply(new RequestOptions()
.optionalCircleCrop()
.placeholder(R.mipmap.default_event_cover_rounded)
.error(R.mipmap.default_event_cover_rounded)
.diskCacheStrategy(DiskCacheStrategy.ALL))
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
imageView.setImageDrawable(resource);
Bitmap icon = iconGenerator.makeIcon();
try {
marker.setIcon(BitmapDescriptorFactory.fromBitmap(icon));
} catch (Exception e) {
Log.d("TAG", "onResourceReady: " + e.getMessage());
}
}
});
}
}
This how you can update your icon after: Define some Map<String, Marker> markers
in top of your CustomClusterRender, initialize it in constructor. 之后,如何更新图标:在CustomClusterRender顶部定义一些
Map<String, Marker> markers
,然后在构造函数中对其进行初始化。 add your marker with some key after you this marker.setIcon
. 在此
marker.setIcon
之后添加带有一些键的标记。 Add like markers.put(clusterItem.getId(), marker);
添加类似
markers.put(clusterItem.getId(), marker);
your custom cluster must have unique id. 您的自定义集群必须具有唯一的ID。 your method will be
您的方法将是
public void updateMarker(final String id, String imgUrl) {
if (markers.size() > 0) {
if (iconImage != null) {
Glide.with(mContext.getApplicationContext())
.load(imgUrl)
.apply(new RequestOptions()
.optionalCircleCrop()
.placeholder(R.mipmap.default_event_cover_rounded)
.error(R.mipmap.default_event_cover_rounded)
.diskCacheStrategy(DiskCacheStrategy.ALL))
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
iconImage.setImageDrawable(resource);
Bitmap icon = iconGenerator.makeIcon();
try {
markers.get(id).setIcon(BitmapDescriptorFactory.fromBitmap(icon));
} catch (Exception e) {
Log.d("TAG_SNAP", "onResourceReady: " + e.getMessage() + markers.get(id).getTitle());
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.