Google Maps' marker moves when zooming

和自甴很熟 提交于 2019-12-23 05:17:11

问题


I've added a mark to my map but when I zoom in it moves to the left (at maximum zoom in it marks the right place) and when I zoom out it moves to the right (at maximum zoom out it marks like 3000Km from the right place).

Here's the class that draws the marker before the onCreate (the image is 62px w x 70px h):

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

            Point screenPts = new Point();
            map.getProjection().toPixels(point, screenPts);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.mapmarker);            
            canvas.drawBitmap(bmp, screenPts.x + 50, screenPts.y - 70, null);         
            return true;
        }
    } 

And here's the code I use to call the class:

        latitud = loc.getLatitude();
            longitud = loc.getLongitude();
            precision = loc.getAccuracy();

        controlMapa = map.getController();
        point = new GeoPoint((int) (latitud * 1E6), (int) (longitud * 1E6));

        controlMapa.animateTo(point);
        controlMapa.setZoom(17);

        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = map.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        map.invalidate();

What's the problem? How can I make them move to the correct point each time zoom changes?

Thank you in advance!


回答1:


Solved! I just had to add:

canvas.drawBitmap(bmp, screenPts.x - (bmp.getWidth() / 2), screenPts.y - bmp.getHeight(), new Paint());

instead of:

canvas.drawBitmap(bmp, screenPts.x + 50, screenPts.y - 70, null); 


来源:https://stackoverflow.com/questions/10813963/google-maps-marker-moves-when-zooming

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