【问题标题】:Can "overlay" size be zoomed together with the google map on android?“覆盖”大小可以与android上的谷歌地图一起缩放吗?
【发布时间】:2012-04-18 02:50:50
【问题描述】:

我已经能够使用“MapActivity”和“ItemizedOverlay”在带有 Eclipse 的 android 上的谷歌地图上绘制叠加层。但是当地图放大和缩小时,叠加层不会改变大小。

我希望叠加层“固定”在地图上,并随地图一起放大和缩小。如何做到这一点?

我找不到定义边界的教程(例如,使用 GPS 坐标作为叠加图像的角)。如果您知道一些,请提供链接。

还有其他方法吗?

非常感谢。

【问题讨论】:

    标签: android google-maps gps overlay itemizedoverlay


    【解决方案1】:

    是的,有。我的申请也有同样的问题。这是一个例子,它完美地工作。当您放大和缩小地图时,此处绘制的圆圈会按比例缩放。

    在你的 Overlay 类中:

    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))));         
    }
    }
    

    当您的叠加层从缩放中重新绘制时,标记将根据 MeterToRadius 比例调整大小。希望对您有所帮助。

    【讨论】:

    • 我正在向地图添加可绘制标记,如何根据缩放级别更改大小
    【解决方案2】:

    看了一圈后,我想出了以下答案(我使用code from this blog 来调整大小)。

    我覆盖了上面使用的相同方法,但有一些不同:

    • 我使用 drawBitmap 方法代替了 drawCircle 方法
    • 我没有使用 Paint 对象
    • 我在覆盖项目上循环以调整标记的大小

    结果是这样的:

     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", "");
            }
      }
    

    不过要小心!顾名思义,currentPoint.getMarker(0) 方法获取当前标记的数据。这意味着您拥有其原始大小,而不是其增长(或缩小)的方式。你必须有一些方法:

    • 存储地图的原始大小
    • 验证自(比率)以来它的变化情况

    我注释掉的代码处理我希望允许的最大和最小调整大小(因此可以看到标记而不会重叠/人眼仍然清晰可见)。

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 2019-03-20
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 2011-05-28
      相关资源
      最近更新 更多