J.yo J.yo - 3 months ago 19
Android Question

openstreetmap - How to set marker in center of screen?

I'm trying to display marker using osmbonuspack_v4.8 ilb.
The marker was displayed but it place in left-top of screen.
I'm trying to set in center but can't. I don't konw the reason.
Please tell me the best way.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);

mapView.setTileSource(TileSourceFactory.MAPNIK);
mapController = (MapController) this.mapView.getController();
mapController.setZoom(15);
GeoPoint center = new GeoPoint(52.221, 6.893);
mapController.animateTo(center);
//mapController.setCenter(center);

//--- Create Overlay
addMarker(center);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

Answer

You can try this way:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
        mapView.setBottom(height);
        mapView.setRight(width);
        mapView.setTileSource(TileSourceFactory.MAPNIK);
        mapController = (MapController) this.mapView.getController();
        mapController.setZoom(15);
        GeoPoint center = new GeoPoint(52.221, 6.893);
        mapController.animateTo(center);
        //mapController.setCenter(center);

        //--- Create Overlay
        addMarker(center);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

I hope that helps!