Can “overlay” size be zoomed together with the google map on android?

不问归期 提交于 2019-12-02 19:41:28

Yes, there is. I had the same issue with my application. Here's an example and it works perfectly. The circle drawn here scales as you zoom in and out of the map.

In your Overlay class:

public class ImpactOverlay extends Overlay {

private static int CIRCLERADIUS = 0;
private GeoPoint geopoint;

public ImpactOverlay(GeoPoint point, int myRadius) {
    geopoint = point;
    CIRCLERADIUS = myRadius;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    // Transfrom geoposition to Point on canvas
    Projection projection = mapView.getProjection();
    Point point = new Point();
    projection.toPixels(geopoint, point);

    // the circle to mark the spot
    Paint circle = new Paint();
    circle.setColor(Color.BLACK);
    int myCircleRadius = metersToRadius(CIRCLERADIUS, mapView, (double)geopoint.getLatitudeE6()/1000000);
    canvas.drawCircle(point.x, point.y, myCircleRadius, circle);
}

public static int metersToRadius(float meters, MapView map, double latitude) {
    return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/ Math.cos(Math.toRadians(latitude))));         
}
}

As your overlay re-draws from zoom, the marker will resize with it based on the metersToRadius scale. Hope it helps.

After a bit of looking around, I came up with the following answer (I used code from this blog to do the resizing).

I overrode the same method used above, with some differences:

  • Instead of the drawCircle method I used the drawBitmap method
  • I didn't use the Paint object
  • I cycled over the overlay items to resize the markers

It ends up like this:

 for(OverlayItem currentPoint:mOverlays){
        geopoint =currentPoint.getPoint();
        Point point = new Point();
        projection.toPixels(geopoint, point);                       
        BitmapDrawable bitDraw= ((BitmapDrawable)currentPoint.getMarker(0));
        Drawable currentMarker = currentPoint.getMarker(0);
        /*if (currentMarker!=null){

            if (currentWidthRatio<.6f) {                
                currentWidthRatio = .6f;                    
            }
            else if (currentWidthRatio>1.5f) {
                currentWidthRatio = 1.5f;
            }
        }*/

        if (bitDraw!=null){         
        Bitmap bitmap = getResizedBitmap(bitDraw.getBitmap(),(int)(currentMarker.getIntrinsicHeight() * currentWidthRatio),
                (int)(currentMarker.getIntrinsicWidth()*currentWidthRatio));

        canvas.drawBitmap(bitmap,
                point.x - bitmap.getHeight()/2,
                point.y - bitmap.getWidth()/2, null);
        }else{
            Log.println(Log.DEBUG,"non-existent point", "");
        }
  }

Careful, though! The currentPoint.getMarker(0) method gets, as its name implies, the current marker's data. That means you have its original size, not the way it has grown (or shrunk) since. You have to have some method to:

  • store the original size of the map
  • verify how it has changed since (ratio)

The code I commented out handles the maximum and minimum resizing I want to allow (so the markers can be seen without overlapping / remain legible to the human eye).

I hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!