How can I use Picasso to add icon to Marker?

前端 未结 3 598
既然无缘
既然无缘 2020-12-05 12:25

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,

相关标签:
3条回答
  • 2020-12-05 12:37

    As @iagreen answer for generating Icon from Bitmap, you can use 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
    }
    
    0 讨论(0)
  • 2020-12-05 12:54

    Picasso provides a generic Target interface you can use to implement your own image destination. Specifically, you will want to override onBitmapLoaded to populate your marker.

    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. Therefore, the marker reference needs to exist until the image is loaded to avoid have the callbacks being cleaned up by the garbage collector.

    0 讨论(0)
  • 2020-12-05 13:00

    Check this google maps sample code, you can find a InfoWindowAdapter implementation to achieve it : googlemaps/android-samples

    0 讨论(0)
提交回复
热议问题