use of org.mapsforge.map.util.MapViewProjection 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();
}
Aggregations