Rincew1nd
Rincew1nd

Reputation: 196

Google maps custom marker android

I'm making a public transportation map based on Google Maps service for Android. The map should contain a lot of markers (over 300) and they should resize when the map is zooming in and out (scale). Right now the markers just overlap each other, is there a way to create custom markers like this?

example picture

I have tried myself, but have had no success. With android-map-utils library (https://github.com/googlemaps/android-maps-utils) markers now look better, but they aren't resizeable and look different.

Criteria: - Markers contain a dot that points on needed location and text on right or left side of dot - Markers should resize with map.

Upvotes: 0

Views: 7986

Answers (1)

ZeroOne
ZeroOne

Reputation: 9117

using bitmapscale

Bitmap markerBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_marker,options);
markerBitmap = SubUtils.scaleBitmap(markerBitmap, 70, 70);

then set it to your Markeroptions

MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(markerBitmap));
Marker mark = googleMap.addMarker(marker);

EDIT

here is scaleBitmap method

public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

        float scaleX = newWidth / (float) bitmap.getWidth();
        float scaleY = newHeight / (float) bitmap.getHeight();
        float pivotX = 0;
        float pivotY = 0;

        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));

        return scaledBitmap;
    }

Upvotes: 1

Related Questions