5

////OSMdroid centered on the markers////

I add markers, I need to map the maximum increases or decreases in such a way that all markers were visible

my code:

public class mapcode extends Activity {
    globalvar appState;
    int stats=0;
    private MapView mapView;
    private IMapController mapController;
    private SimpleLocationOverlay mMyLocationOverlay;
    private ScaleBarOverlay mScaleBarOverlay;  
    ItemizedIconOverlay<OverlayItem> currentLocationOverlay;
    DefaultResourceProxyImpl resourceProxy;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.map);
        
        appState = ((globalvar) getApplicationContext());

        
        
        
        
        mapView = (MapView) this.findViewById(R.id.mapview);  
        mapView.setTileSource(TileSourceFactory.MAPNIK);
      //  mapView.setBuiltInZoomControls(true); //кнопка ZOOM +-
        mapView.setMultiTouchControls(true);
        
        mapController = this.mapView.getController();
        mapController.setZoom(2);
        
        this.mMyLocationOverlay = new SimpleLocationOverlay(this);                          
        this.mapView.getOverlays().add(mMyLocationOverlay);
        
        this.mScaleBarOverlay = new ScaleBarOverlay(this);                          
        this.mapView.getOverlays().add(mScaleBarOverlay);
//      this.mapView
        
        /////////////////
        resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
        GeoPoint  currentLocation = new GeoPoint(55.860863,37.115046); 
        GeoPoint  currentLocation2 = new GeoPoint(63.557413,-156.102119); 
      
        
        OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation);
        Drawable myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.a);
        myLocationOverlayItem.setMarker(myCurrentLocationMarker);
       // myLocationOverlayItem.setMarkerHotspot(HotspotPlace.CENTER); //no working/
        
        final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
        items.add(myLocationOverlayItem);
        
        
        myLocationOverlayItem = new OverlayItem("Here", "Current Position", currentLocation2);
        myCurrentLocationMarker = this.getResources().getDrawable(R.drawable.a);
        myLocationOverlayItem.setMarker(myCurrentLocationMarker);

  //    myLocationOverlayItem.setMarkerHotspot(HotspotPlace.CENTER); // no working

        
        items.add(myLocationOverlayItem);
        
        

        currentLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                        return true;
                    }
                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                        return true;
                    }
                }, resourceProxy);
        
        
        this.mapView.getOverlays().add(this.currentLocationOverlay);
}

I added two markers, but only one is visible:

image with one marker

and I need to osmdroid is centered and immediately showed both marker

image with two marker

1
  • 2
    You could use the zoomToBoundingBox method, but you would need to go through all of your markers to determine the top, left, bottom, and right coordinates. May 25, 2014 at 0:22

2 Answers 2

12

I think you want something like this:

int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLong = Integer.MIN_VALUE;
ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
for (OverlayItem item : items) {
    GeoPoint point = item.getPoint();
    if (point.getLatitudeE6() < minLat)
        minLat = point.getLatitudeE6();
    if (point.getLatitudeE6() > maxLat)
        maxLat = point.getLatitudeE6();
    if (point.getLongitudeE6() < minLong)
        minLong = point.getLongitudeE6();
    if (point.getLongitudeE6() > maxLong)
        maxLong = point.getLongitudeE6();
}

BoundingBoxE6 boundingBox = new BoundingBoxE6(maxLat, maxLong, minLat, minLong);
mMapView.zoomToBoundingBox(boundingBox);
2
  • 1
    adding mapController = mMapView.getController(); mapController.zoomToSpan(boundingBox.getLatitudeSpanE6(), boundingBox.getLongitudeSpanE6()); mapController.setCenter(boundingBox.getCenter()); after mMapView.zoomToBoundingBox(boundingBox); works perfectly fine for me.
    – Jei
    Jul 4, 2017 at 7:09
  • 1
    Unable to see all markers even i set the BoundingBox boundingBox = new BoundingBox(north, west, south, east); boundingBox.getLongitudeSpanWithDateLine(); mMapView.zoomToBoundingBox(boundingBox, true);
    – Harish
    Oct 31, 2019 at 7:12
0

You can calculate boundingBox by BoundingBox.fromGeoPoints(geoPoints)

fun zoomToBounds(points: List<LatLng>) {
    val geoPoints = points.map { GeoPoint(it.latitude, it.longitude) }
    val boundingBox = BoundingBox.fromGeoPoints(geoPoints)
    mapView.zoomToBoundingBox(boundingBox, true)
}

It is also possible to have some paddings by using zoomtoBoundingBox overloaded method and setting the pBorderSizeInPixels parameter

public double zoomToBoundingBox(
    final BoundingBox pBoundingBox, 
    final boolean pAnimated, 
    final int pBorderSizeInPixels, 
    final double pMaximumZoom, 
    final Long pAnimationSpeed
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.