问题
I trying to integrate JxMapViewer into my swing application.
I have drawn a line on the map between two way points. Now I wish to implement a functionality like:
Wherever I am on a map, whatever zoom level is set, whenever i click on a button (or some external event), focus should be shifted to line i have drawn (i.e. indirectly on the two waypoints)
Secondly, the zoom level should be set such that portion of a map containing those two waypoints should be visible.
I see that we have getViewportBounds
method inside JxMApViewer
class. Similarly we do not have setViewportBounds
. In that case we could have calculated a rectangle which encompasses those two waypoints and set it.
Any hint would be appreciated
I have modified existing calculateZoomFrom
method as:
int zoom = mainMap.getZoom();
Rectangle2D inner = generateBoundedRectangleForLine(positionSet, mainMap.getZoom());
int count = 0;
Rectangle outer = mainMap.getViewportBounds();
while(!outer.contains(inner) || (outer.contains(inner) && !(inner.getHeight() >= mainMap.getHeight() - 50))) {
Point2D center = new Point2D.Double(
inner.getX() + inner.getWidth()/2,
inner.getY() + inner.getHeight()/2);
GeoPosition px = mainMap.getTileFactory().pixelToGeo(center,zoom);
mainMap.setCenterPosition(px);
count++;
if(count > 30) break;
if(outer.contains(inner)) {
if (!(inner.getHeight() >= mainMap.getHeight() - 50)) {
zoom = zoom - 1;
if(zoom < 1) {
break;
}
} else {
break;
}
} else {
zoom = zoom + 1;
if(zoom > 15) {
break;
}
}
mainMap.setZoom(zoom);
inner = generateBoundedRectangleForLine(positionSet, zoom);
}
But it is not working.
回答1:
You can call JXMapViewer.setCenterPosition(GeoPosition geoPosition)
from your ActionListener
for that.
You would have to calculate the appropriate zoom level yourself (JXMapViewer doesn't offer anything for that as far as I know). Since you know the coordinates of the waypoint, calculating the bounding box and setting the appropriate zoom level shouldn't be a problem.
回答2:
I found the solution. Solution was to set zoom level to the lowest one and then call calculateZoomFrom method.
来源:https://stackoverflow.com/questions/10959703/swing-jxmapviewer-focus-on-a-object-and-set-corresponding-zoom-level