Change marker size in Google Maps API v2

后端 未结 5 2105
故里飘歌
故里飘歌 2020-12-05 09:59

I\'m trying to port my app to the brand new Google Maps API v2, but can\'t find how to change the size of the marker (some of my markers are smaller than default).

相关标签:
5条回答
  • 2020-12-05 10:34

    It seems the only way to do it is by setting a custom Marker image.

    From API Reference: If you'd like to change more than just the color of the marker, you can set a custom marker image, often called an icon. Custom icons are always set as a BitmapDescriptor, and defined using one of four methods in the BitmapDescriptorFactory class.

    0 讨论(0)
  • 2020-12-05 10:41

    You can first convert it into Bitmap and change its size and then use that bitmap in as a custom marker. For example I first created a method which accepts name of your image file in drawable folder, and width and height of marker you want to set.

    public Bitmap resizeMapIcons(String iconName,int width, int height){
        Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(),getResources().getIdentifier(iconName, "drawable", getPackageName()));
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap, width, height, false);
        return resizedBitmap;
    }
    

    Then call like this in setUpMap() method to create a new marker of required size.

    googleMap.addMarker(new MarkerOptions()
                .title("New Marker")
                .snippet("Check out this place.")
                .position(chelsea).icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("image_name",100,100))));
    
    0 讨论(0)
  • 2020-12-05 10:46

    Best solution I've found is to resize the Bitmap just before adding it as a Marker. For example, in my code I use a LevelListDrawable which references several multiple-resolution Drawables. Since I want half-size Markers, I do:

    LevelListDrawable d=(LevelListDrawable) getResources().getDrawable(R.drawable.estado_variable);
    d.setLevel(1234);
    BitmapDrawable bd=(BitmapDrawable) d.getCurrent();
    Bitmap b=bd.getBitmap();
    Bitmap bhalfsize=Bitmap.createScaledBitmap(b, b.getWidth()/2,b.getHeight()/2, false);
    mapa.addMarker(new MarkerOptions()
            .position(POSITION)
            .title("Title")
            .icon(BitmapDescriptorFactory.fromBitmap(bhalfsize))
            );
    

    This way, I can keep using Drawables while been able to obtain differently sized markers just converting them to Bitmap and resizing as needed.

    0 讨论(0)
  • 2020-12-05 10:55
    public Bitmap bitmapSizeByScall( Bitmap bitmapIn, float scall_zero_to_one_f) {
    
        Bitmap bitmapOut = Bitmap.createScaledBitmap(bitmapIn,
                Math.round(bitmapIn.getWidth() * scall_zero_to_one_f),
                Math.round(bitmapIn.getHeight() * scall_zero_to_one_f), false);
    
        return bitmapOut;
    }
    

    Bitmap size returns to 80% of the original.

    Bitmap resizeBitmap = bitmapSizeByScall(originBitmap, 0.8f);
    
    0 讨论(0)
  • 2020-12-05 10:55

    Just a quick snippet that works for me:

    private Bitmap scaleImage(Resources res, int id, int lessSideSize) {
        Bitmap b = null;
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
    
        BitmapFactory.decodeResource(res, id, o);
    
        float sc = 0.0f;
        int scale = 1;
        // if image height is greater than width
        if (o.outHeight > o.outWidth) {
            sc = o.outHeight / lessSideSize;
            scale = Math.round(sc);
        }
        // if image width is greater than height
        else {
            sc = o.outWidth / lessSideSize;
            scale = Math.round(sc);
        }
    
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        b = BitmapFactory.decodeResource(res, id, o2);
        return b;
    }
    
    0 讨论(0)
提交回复
热议问题