将图像叠加到 MapView 上并在缩放更改时刷新其位置

发布于 2024-12-08 12:44:48 字数 2508 浏览 0 评论 0原文

我有一个符合路线的地理点列表。我创建了一个类,在起点绘制星形 png 图像,在终点绘制 F1 标志 (meta.png)。

public class MapTestActivity extends MapActivity {
...
class Marcador extends com.google.android.maps.Overlay {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(ruta.get(0), screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.start);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   

            // Otra vez para la meta:
          //---translate the GeoPoint to screen pixels---
            screenPts = new Point();
            mapView.getProjection().toPixels(ruta.get(ruta.size()-1), screenPts);

            //---add the marker---
            bmp = BitmapFactory.decodeResource(getResources(), R.drawable.meta);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null); 

            return true;
        }
        }
...
}

在 onCreate 方法中,我如下进行:

public class MapTestActivity extends MapActivity {
...
 private List<GeoPoint> ruta;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MapView mapView = (MapView)findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        MapController mc = mapView.getController();

        ruta = new ArrayList<GeoPoint>();

        ruta.add(new GeoPoint((int) (43.30782*1E6), (int) (-5.69379*1E6)));
        ruta.add(new GeoPoint((int) (43.30785*1E6), (int) (-5.69435*1E6)));
        ruta.add(new GeoPoint((int) (43.30832*1E6), (int) (-5.69422*1E6)));
        ruta.add(new GeoPoint((int) (43.30894*1E6), (int) (-5.69538*1E6)));

        mc.animateTo(ruta.get(0));
        mc.setZoom(10);


        // PNG images:
        Marcador marcadoresSalidaYMeta = new Marcador();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(marcadoresSalidaYMeta);

        mapView.invalidate();
        }
...
}

图像出现在地图上,但当缩放处于最大值时,它们仅出现在正确的坐标处。当我缩小时,它们不精确并且看起来离正确的位置很远。

每次缩放变化时如何使它们移动到正确的点?如果我从值 10 开始,为什么它们适合最大缩放值?

I have a list of GeoPoints conforming a route. I have created a class to paint a star png image at the start point, and a F1 flag (meta.png) at the end point.

public class MapTestActivity extends MapActivity {
...
class Marcador extends com.google.android.maps.Overlay {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(ruta.get(0), screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.start);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);   

            // Otra vez para la meta:
          //---translate the GeoPoint to screen pixels---
            screenPts = new Point();
            mapView.getProjection().toPixels(ruta.get(ruta.size()-1), screenPts);

            //---add the marker---
            bmp = BitmapFactory.decodeResource(getResources(), R.drawable.meta);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null); 

            return true;
        }
        }
...
}

At the onCreate method, I proceed like this:

public class MapTestActivity extends MapActivity {
...
 private List<GeoPoint> ruta;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MapView mapView = (MapView)findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        MapController mc = mapView.getController();

        ruta = new ArrayList<GeoPoint>();

        ruta.add(new GeoPoint((int) (43.30782*1E6), (int) (-5.69379*1E6)));
        ruta.add(new GeoPoint((int) (43.30785*1E6), (int) (-5.69435*1E6)));
        ruta.add(new GeoPoint((int) (43.30832*1E6), (int) (-5.69422*1E6)));
        ruta.add(new GeoPoint((int) (43.30894*1E6), (int) (-5.69538*1E6)));

        mc.animateTo(ruta.get(0));
        mc.setZoom(10);


        // PNG images:
        Marcador marcadoresSalidaYMeta = new Marcador();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(marcadoresSalidaYMeta);

        mapView.invalidate();
        }
...
}

Images appear over the map, but they only appear at the right coordinates when zoom is at its maximum value. When I zoom out, they are imprecise and appear far away from its right place.

How can I make them move to the correct point each time zoom changes? And why do they fit right for the maximum zoom value if I am starting with value 10?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文