简体   繁体   中英

Google Maps' marker moves when zooming

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 wx 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM