Search code examples
androidmapsosmdroidmarkers

OSMDroid: centering a marker


I have a project using OSMDroid. There is a loop that draws markers, end after that I have to center map at a certain marker. My code:

//I have on open Cursor c
Marker switchTo = null;
do {
    Marker mr = new Marker(mapView);
    GeoPoint gp = new GeoPoint(c.getDouble(iLat), c.getDouble(iLng));
    mr.setPosition(gp);
    //some actions: set anchor, drawable etc
    int id = c.getInt(iId);
    if (MainActivity.switchToId != -1 && id == MainActivity.switchToId) {
        switchTo = mr;    //if I have to switch to exactly this marker
        MainActivity.switchToId = -1; //cleanup
    }
    mPoiMarkers.add(mr); //add to cluster
} while (c.moveToNext()); //next marker
mapView.getOverlays().add(mPoiMarkers); //add cluster to map
Log.d(TAG, "Markers redrawed.");

if (switchTo != null) { //go to marker
    mapView.getController().setCenter(switchTo.getPosition());
    mapView.getController().setZoom(14);
}

The problem: map sets centre not at a marker, but in some kilometres from it. I don't know what's going on. Marker stands at its normal place...


Solution

  • The true way is to set centre after setting zoom.

    mapView.getController().setZoom(15);
    mapView.getController().setCenter(sw);