Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image with Glide to Google Maps Marker

I have an issue. I use Glide 3.8.0.

I need to download image from server and put it to a marker on google maps.

    Glide.with(getBaseActivity())
                .load(place.getIconUrl())
                .asBitmap()
                .fitCenter()
                .into(new SimpleTarget<Bitmap>(50,50) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        map.addMarker(new MarkerOptions()
                                .icon(BitmapDescriptorFactory.fromBitmap(resource))
                                .position(place.getLatLng()));
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        map.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_default_logo))
                        .position(place.getLatLng()));
                    }
                });

Also, i have default logo if some loading error, its size 50x50.

I test loading on 2 devices - nexus 5 and no name device (i dont know screen resolution and screen size, but size is almost same as nexus 5)

On the different device i have different size of marker logo and i experiment with

.into(new SimpleTarget<Bitmap>(50,50) sizes

Nexus 5, 50x50 very smaller comparing with default logo, 75x75 is smaller then default, 150x150 same as default

No name device: 75x75 same as default logo, 50x50 smaller then default

So, what can i do with Glide to make it same on the different device and as same as default logo 50x50 (default logo looks same on the different devices)

Nexus 5 50x50

Nexus 5  75x75

Nexus 5 150x150

like image 285
pligosv Avatar asked Feb 21 '18 13:02

pligosv


People also ask

How do I start a custom image on Google Maps marker for mobile app?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.


2 Answers

 Glide.with(this).load("url").listener(object : RequestListener<Drawable> {
        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {


          return true
        }

        override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
            callmethod(resource)     //pass drawable to your method and set the drawable for marker there
            imageSource=resource
            return true
        }

    })

using

  BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(convertToBitmap(d,100,100));
  MarkerOptions markerOptions = new MarkerOptions()
            .position(latLng).icon(icon)
            .title(getString(titleResId))
            .draggable(true);

also for getting the bitmap from drawable

 public Bitmap convertToBitmap(Drawable drawable, int widthPixels, int heightPixels) {
    Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mutableBitmap);
    drawable.setBounds(0, 0, widthPixels, heightPixels);
    drawable.draw(canvas);

    return mutableBitmap;
}

or you can use just

public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
like image 165
3 revs Avatar answered Oct 11 '22 06:10

3 revs


Decided with recreate bitmap

Glide.with(getBaseActivity())
                .load(place.getIconUrl())
                .asBitmap()
                .dontTransform()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        final float scale = getContext().getResources().getDisplayMetrics().density;
                        int pixels = (int) (50 * scale + 0.5f);
                        Bitmap bitmap = Bitmap.createScaledBitmap(resource, pixels, pixels, true);
                        markerImageView.setImageBitmap(bitmap);
                        addMarker(place.getLatLng());
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        markerImageView.setImageResource(R.drawable.ic_marker_default_logo);
                        addMarker(place.getLatLng());
                    }
                });
like image 2
pligosv Avatar answered Oct 11 '22 05:10

pligosv