Skip to main content

calculateZoomFrom

2 replies [Last post]
page80
Offline
Joined: 2009-11-24
Points: 0

Anybody know how to use this method? It takes a Set of GeoPositions and returns nothing. It's supposed to calculate "a zoom level so that all points in the specified set will be visible on screen." It's part of JXMapViewer.

Thanks in advance.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
catdriver
Offline
Joined: 2011-12-15
Points: 0

I would like to know the answer too.

page80
Offline
Joined: 2009-11-24
Points: 0

I made some changes to the methods setZoom and calculateZoomFrom. It was quite awhile ago some I don't remember exactly what was changed, other than adding a max zoom variable rather than the hard coded one of 19, but here is the code for the methods.

public void calculateZoomFrom(Set<GeoPosition> positions, int maxZoomLevel) {
if (positions.size() < 2) {
return;
}
int tZoom = maxZoomLevel - getZoom();
Rectangle2D rect = generateBoundingRect(positions, tZoom);
Point2D tCenter = null;
GeoPosition px = null;
int count = 0;
try {
while (!getViewportBounds().contains(rect)) {
tCenter = new Point2D.Double(
rect.getX() + rect.getWidth() / 2,
rect.getY() + rect.getHeight() / 2);
px = getTileFactory().pixelToGeo(tCenter, tZoom);
setCenterPosition(px);
count++;
if (count > 30) {
break;
}
if (getViewportBounds().contains(rect)) {
break;
}
tZoom = tZoom + 1;
if (tZoom > 15) {
break;
}
setZoom(tZoom);
rect = generateBoundingRect(positions, tZoom);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
rect = null;
tCenter = null;
px = null;
}
}

public void setZoom(int zoom) {
if (zoom == this.zoom) {
return;
}

TileFactoryInfo info = getTileFactory().getInfo();
// don't repaint if we are out of the valid zoom levels
if (info != null
&& (zoom < info.getMinimumZoomLevel()
|| zoom > info.getMaximumZoomLevel())) {
return;
}

int oldzoom = this.zoom;
Point2D oldCenter = getCenter();
Dimension oldMapSize = getTileFactory().getMapSize(oldzoom);
this.zoom = zoom;
this.firePropertyChange("zoom", oldzoom, zoom);

Dimension mapSize = getTileFactory().getMapSize(zoom);

setCenter(new Point2D.Double(
oldCenter.getX() * (mapSize.getWidth() / oldMapSize.getWidth()),
oldCenter.getY() * (mapSize.getHeight() / oldMapSize.getHeight())));

repaint();
}