MapController setCenter doesn't work

霸气de小男生 提交于 2019-12-11 04:07:02

问题


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

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

回答1:


"What is wrong?"

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

Upgrade to v4.3.




回答2:


I know that the question is relatively old and already answered, but for those who work in Xamarin and are constrained due to the Xamarin Component which is outdated to use the 4.2 version please use the solution below, thanks to Tony of course..

GeoPoint gp = new GeoPoint(lat, lng);
ViewTreeObserver vto = _mapView.ViewTreeObserver;
vto.GlobalLayout += (sender, args) =>
{
    _mapView.Controller.SetCenter(gp);
};



回答3:


It is probably because you called setCenter() before the map was laid out.

Try one of those options:

  1. call setCenter() somewhere in the code where you can be sure that the map is laid out
  2. Override the onLayout of the map
  3. trigger an async task

    @SuppressLint("StaticFieldLeak") AsyncTask<Void,Void,Void> waitForSize = new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... voids) {
        while(mapView.getWidth() == 0){
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
            }
        }
        return null;
    }
    
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        mapView.getController().setCenter(myLocation);
    }
    

    };

waitForSize.execute();



来源:https://stackoverflow.com/questions/28719221/mapcontroller-setcenter-doesnt-work

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