Android Google Maps resize marker on Zoom

2019-07-21 05:36发布

问题:

I need to display around 30 markers in a somewhat small area.

Is there a way to resize Google Maps API V2 markers when the user changes the zoom?

Or should I just use a smaller image?

Thanks in advance!

回答1:

If you want to do something when zooming, you can create a custom Mapview which extends the original MapView, and just override dispatchDraw(Canvas canvas).

With adding a little listener, you can do whatever you want in the callback. Something like this;

    @Override
protected void dispatchDraw(final Canvas canvas) {
    super.dispatchDraw(canvas);
    if (getZoomLevel() != lastZoomLevel) {
        if (listener != null) {
            listener.onZoom(lastZoomLevel, getZoomLevel());
        }
        lastZoomLevel = getZoomLevel();
    }         
}


回答2:

There is no an outOfTheBox option to change the icons for the markers. you will have to listen to zoom changes:

How to listen for user generated zoom in Google Maps?

and to remove the older markers and set new ones in their location with smaller icon images.



回答3:

I use extends and fitBounds functions and get positions (LatLng) to set a zoom to map items

//JSP

<c:forEach var="lastPosition" items="${lastPositionResultDto}" >        
    <input type="hidden" id="mapPointsPosition_${lastPosition.id}" value="${lastPosition.latitude}_${lastPosition.longitude}" >
</c:forEach>


//JAVASCRIPT

map = new google.maps.Map(document.getElementById('map-canvas-position'), mapOptions);    

var arrPointMaps = $("[id^=mapPointsPosition_]");
var limits = new google.maps.LatLngBounds(0,0);

for(var j=0; j<arrPointMaps.length;  j++){

    var stringPointMap = arrPointMaps[j].getAttribute("value").split("_");
    var latAlertPoint = stringPointMap[0];
    var lonAlertPoint = stringPointMap[1];                

    var limitPosition = new google.maps.LatLng(parseFloat(latAlertPoint), parseFloat(lonAlertPoint));
    limits.extend(limitPosition);

}   

map.fitBounds(limits); 

}