Search code examples
javaandroidopenstreetmaposmdroid

MapController setCenter doesn't work


I've got a GeoPoint gPt with 55.790833, 49.114444 coordinates. I add that thing to map and try to center. But my marker is not in the center of map (it's a little bit higher). enter image description here enter image description here

What is wrong?

This is my code:

mResourceProxy = new ResourceProxyImpl(this.getActivity().getApplicationContext());
        mapView = (MapView) v.findViewById(R.id.mapview);
        mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapView.setBuiltInZoomControls(true);
        mMapController = mapView.getController();
        mMapController.setZoom(15);
        GeoPoint gPt = new GeoPoint(55.790833, 49.114444);
        ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();        
        items.add(new OverlayItem("Here", "SampleDescription", gPt));

        mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    @Override
                    public boolean onItemSingleTapUp(final int index,
                            final OverlayItem item) {

                        return true; // We 'handled' this event.
                    }

                    @Override
                    public boolean onItemLongPress(final int index,
                            final OverlayItem item) {

                        return false;
                    }
                }, mResourceProxy);
        mapView.getOverlays().add(this.mMyLocationOverlay);
        mapView.invalidate();

        mMapController.setCenter(gPt);  

EDIT:

If you are feeling lazy to update osmdroid, just add next statements:

ViewTreeObserver vto = v.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new       ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mapView.getController().setCenter(gPt);
            }
        });

Solution

  • "What is wrong?"

    => Certainly your version of osmdroid, with issue #22.

    Upgrade to v4.3.