gpt4 book ai didi

java - 在java android谷歌地图中显示和更新标记的图标

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:42 25 4
gpt4 key购买 nike

我使用github上的代码(这不是我的代码)作为示例 https://github.com/mitchtabian/Google-Maps-2018/tree/displaying-trip-duration-of-polylines-end

这是我的问题:

  • 假设有一些用户(标记者)。
  • 我想将标记的图标更改为互联网上的另一张图片,但它不起作用。

我有两个问题:

  1. onBeforeClusterItemRendered() 方法不显示图像。如果我像原始示例一样使用资源中的图像,那么它就可以工作。
  2. 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));
    }
    }
    }

更新:我也展示了这个类的构造函数:

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

}

最佳答案

您可以在 onClusterItemRendered() 方法中使用您的方法。看看这个答案 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));
}

------ 在此处更新如何使用 glide(我的版本 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());
}
}
});
}
}

您可以通过以下方式更新图标:定义一些Map<String, Marker> markers在 CustomClusterRender 的顶部,在构造函数中对其进行初始化。在 marker.setIcon 之后添加一些带有键的标记。添加如 markers.put(clusterItem.getId(), marker);您的自定义集群必须具有唯一的 ID。你的方法将是

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

关于java - 在java android谷歌地图中显示和更新标记的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55566615/

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