Search in sources :

Example 1 with BoundingBox

use of org.mapsforge.core.model.BoundingBox in project RSAndroidApp by RailwayStations.

the class ClusterManager method getCurBounds.

/**
 * get the current BoundingBox of the viewport
 *
 * @return current BoundingBox of the viewport
 */
protected synchronized BoundingBox getCurBounds() {
    if (currBoundingBox == null) {
        if (mapView == null) {
            throw new NullPointerException("mapView == null");
        }
        if (mapView.getWidth() <= 0 || mapView.getHeight() <= 0) {
            throw new IllegalArgumentException(" mapView.getWidth() <= 0 " + "|| mapView.getHeight() <= 0 " + mapView.getWidth() + " || " + mapView.getHeight());
        }
        /* North-West geo point of the bound */
        final var nw_ = mapView.getMapViewProjection().fromPixels(-mapView.getWidth() * 0.5, -mapView.getHeight() * 0.5);
        /* South-East geo point of the bound */
        final var se_ = mapView.getMapViewProjection().fromPixels(mapView.getWidth() + mapView.getWidth() * 0.5, mapView.getHeight() + mapView.getHeight() * 0.5);
        if (se_ != null && nw_ != null) {
            if (se_.latitude > nw_.latitude) {
                currBoundingBox = new BoundingBox(nw_.latitude, se_.longitude, se_.latitude, nw_.longitude);
            } else {
                currBoundingBox = new BoundingBox(se_.latitude, nw_.longitude, nw_.latitude, se_.longitude);
            }
        }
    }
    return currBoundingBox;
}
Also used : BoundingBox(org.mapsforge.core.model.BoundingBox)

Example 2 with BoundingBox

use of org.mapsforge.core.model.BoundingBox in project satstat by mvglasow.

the class MapSectionFragment method updateMap.

/**
 * Updates the map view so that all markers are visible.
 */
public void updateMap() {
    boolean needsRedraw = false;
    Dimension dimension = mapMap.getModel().mapViewDimension.getDimension();
    // just trigger a redraw if we're not going to pan or zoom
    if ((dimension == null) || (!isMapViewAttached)) {
        mapMap.getLayerManager().redrawLayers();
        return;
    }
    // move locations into view and zoom out as needed
    int tileSize = mapMap.getModel().displayModel.getTileSize();
    BoundingBox bb = null;
    BoundingBox bb2 = null;
    for (Location l : providerLocations.values()) if ((l != null) && (l.getProvider() != "")) {
        double lat = l.getLatitude();
        double lon = l.getLongitude();
        double yRadius = l.hasAccuracy() ? ((l.getAccuracy() * 360.0f) / Const.EARTH_CIRCUMFERENCE) : 0;
        double xRadius = l.hasAccuracy() ? (yRadius * Math.abs(Math.cos(lat))) : 0;
        double minLon = Math.max(lon - xRadius, -180);
        double maxLon = Math.min(lon + xRadius, 180);
        double minLat = Math.max(lat - yRadius, -90);
        double maxLat = Math.min(lat + yRadius, 90);
        if (!isLocationStale(l)) {
            // location is up to date, add to main BoundingBox
            if (bb != null) {
                minLat = Math.min(bb.minLatitude, minLat);
                maxLat = Math.max(bb.maxLatitude, maxLat);
                minLon = Math.min(bb.minLongitude, minLon);
                maxLon = Math.max(bb.maxLongitude, maxLon);
            }
            bb = new BoundingBox(minLat, minLon, maxLat, maxLon);
        } else {
            // location is stale, add to stale BoundingBox
            if (bb2 != null) {
                minLat = Math.min(bb2.minLatitude, minLat);
                maxLat = Math.max(bb2.maxLatitude, maxLat);
                minLon = Math.min(bb2.minLongitude, minLon);
                maxLon = Math.max(bb2.maxLongitude, maxLon);
            }
            bb2 = new BoundingBox(minLat, minLon, maxLat, maxLon);
        }
    }
    // all locations are stale, center to them
    if (bb == null)
        bb = bb2;
    if (bb == null) {
        needsRedraw = true;
    } else {
        byte newZoom = LatLongUtils.zoomForBounds(dimension, bb, tileSize);
        if (newZoom < 0)
            newZoom = 0;
        if (newZoom < mapMap.getModel().mapViewPosition.getZoomLevel()) {
            mapMap.getModel().mapViewPosition.setZoomLevel(newZoom);
        } else {
            needsRedraw = true;
        }
        MapViewProjection proj = new MapViewProjection(mapMap);
        Point nw = proj.toPixels(new LatLong(bb.maxLatitude, bb.minLongitude));
        Point se = proj.toPixels(new LatLong(bb.minLatitude, bb.maxLongitude));
        // move only if bb is not entirely visible
        if ((nw.x < 0) || (nw.y < 0) || (se.x > dimension.width) || (se.y > dimension.height)) {
            mapMap.getModel().mapViewPosition.setCenter(bb.getCenterPoint());
        } else {
            needsRedraw = true;
        }
    }
    if (needsRedraw)
        mapMap.getLayerManager().redrawLayers();
}
Also used : MapViewProjection(org.mapsforge.map.util.MapViewProjection) BoundingBox(org.mapsforge.core.model.BoundingBox) Dimension(org.mapsforge.core.model.Dimension) Point(org.mapsforge.core.model.Point) LatLong(org.mapsforge.core.model.LatLong) Point(org.mapsforge.core.model.Point) Paint(org.mapsforge.core.graphics.Paint) Location(android.location.Location)

Aggregations

BoundingBox (org.mapsforge.core.model.BoundingBox)2 Location (android.location.Location)1 Paint (org.mapsforge.core.graphics.Paint)1 Dimension (org.mapsforge.core.model.Dimension)1 LatLong (org.mapsforge.core.model.LatLong)1 Point (org.mapsforge.core.model.Point)1 MapViewProjection (org.mapsforge.map.util.MapViewProjection)1