Scale Map Markers by Zoom, Android

2020-06-17 06:43发布

问题:

I have lot of markers on my map. Zooming in each marker shows me a position. but zooming out the markers are overlapping each other and it is more difficult do determine the position of a marker.

Is there a way to scale the marker image depending on the zoom factor?

回答1:

Depends on how you draw your marker. If your marker is just a bitmap/drawable, simply use matrix.postScale()

Matrix matrix = new Matrix();
matrix.postScale(2f, 2f); // douple the size
canvas.drawBitmap(mBitmap, matrix, mPaint);


回答2:

Markers do not change size based on zoom. Use GroundOverlays if you desire this effect. src

 GoogleMap map = ...; // get a map.
 BitmapDescriptor image = ...; // get an image.
 LatLngBounds bounds = ...; // get a bounds
 // Adds a ground overlay with 50% transparency.
 GroundOverlay groundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
     .image(image)
     .positionFromBounds(bounds)
     .transparency(0.5));

A ground overlay is an image that is fixed to a map and scales with the map zoom. Please see the API for more info



回答3:

A simple form is.

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.raw.ic_car_1);
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap); 
        googleMap.addGroundOverlay(new GroundOverlayOptions()
             .image(bitmapDescriptor)
             .position(latLng,100));