简体   繁体   English

如何使用 Picasso 将图标添加到 Marker?

[英]How can I use Picasso to add icon to Marker?

I would like to use Picasso to retrieve the Bitmap image to use as a marker icon but I am not sure how to do so.我想使用毕加索来检索位图图像以用作标记图标,但我不知道该怎么做。 If I am using Picasso to insert an image into an image view, I know I can use:如果我使用 Picasso 将图像插入到图像视图中,我知道我可以使用:

 Picasso.with(MainActivity.this).load(URL).into(photo_imageview);

Of course this will not work if I pass it to .icon()当然,如果我将它传递给.icon()这将.icon()

Is there a simple way to achieve this?有没有简单的方法来实现这一目标?

Thanks to anyone taking a look at this!感谢任何人看这个!

Picasso provides a generic Target interface you can use to implement your own image destination. Picasso 提供了一个通用的Target接口,您可以使用它来实现您自己的图像目标。 Specifically, you will want to override onBitmapLoaded to populate your marker.具体来说,您需要覆盖onBitmapLoaded来填充您的标记。

A basic implementation is given below.下面给出一个基本的实现。

public class PicassoMarker implements Target {
    Marker mMarker;

    PicassoMarker(Marker marker) {
        mMarker = marker;
    }

    @Override
    public int hashCode() {
        return mMarker.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if(o instanceof PicassoMarker) {
            Marker marker = ((PicassoMarker) o).mMarker;
            return mMarker.equals(marker);
        } else {
            return false;
        }
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
}

You would use it like this --你会像这样使用它——

marker = new PicassoMarker(myMarker);
Picasso.with(MainActivity.this).load(URL).into(marker);

Note Picasso only holds a week reference to the Target passed to into .毕加索仅持有一个星期参考Target传递给into Therefore, the marker reference needs to exist until the image is loaded to avoid have the callbacks being cleaned up by the garbage collector.因此,在加载图像之前, marker引用需要存在,以避免垃圾回收器清除回调。

检查这个谷歌地图示例代码,你可以找到一个InfoWindowAdapter实现来实现它: googlemaps/android-samples

As @iagreen answer for generating Icon from Bitmap, you can use Iconfactory .作为从位图生成图标的@iagreen 答案,您可以使用Iconfactory

Kotlin Version:科特林版本:

class PicassoMarker(
val marker: Marker,
private val iconFactory: IconFactory) : Target {
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
    Timber.d("picassoMarker onPrepareLoad : ")
}

override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
    Timber.e("picassoMarker onBitmapFailed: ")
}

override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
    try {
        if (bitmap != null) {
            marker.icon = iconFactory.fromBitmap(bitmap)
        }
    } catch (ex: IllegalArgumentException) {
        Timber.e(ex, "Marker is dead")
    }
}

override fun equals(other: Any?): Boolean {
    if (other is PicassoMarker) {
        return marker == other
    }
    return false
}

override fun hashCode(): Int {
    return marker.hashCode()
}}

And use like this :并像这样使用:

 fun addMarker(marker: Marker): com.mapbox.mapboxsdk.annotations.Marker {
    val markerOption = MarkerOptions().position(marker.latLng)

    markerOption.icon =
        iconFactory
            .fromBitmap(
                Bitmap.createBitmap(
                    1,
                    1,
                    Bitmap.Config.ARGB_8888
                )
            )

    val iconFactory = IconFactory.getInstance(context)

    val picassoMarker = PicassoMarker(
        mapProvider.map.addMarker(markerOption),
        iconFactory
    )

    try {
        val picasso = Picasso.get().apply {
            this.isLoggingEnabled = true
        }

        picasso.load(marker.iconUrl)
            .noPlaceholder()
            .into(picassoMarker)
    } catch (ex: Exception) {
        Timber.e(ex, "Picasso crashed")
    }

    return picassoMarker.marker
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM