0

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.

scai
  • 20,297
  • 4
  • 56
  • 72
user613114
  • 2,731
  • 11
  • 47
  • 73

2 Answers2

2

I found the solution. Solution was to set zoom level to the lowest one and then call calculateZoomFrom method.

user613114
  • 2,731
  • 11
  • 47
  • 73
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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Jasper Siepkes
  • 1,412
  • 16
  • 25
  • 1
    JXMapViewer provides the method calculateZoomFrom(Set positions) which helps me to shift my focus to the center and adjust zoom level such that my waypoints are inside view port bounds, For view port bounds it creates outer rectangle. for waypoint bounds it creates inner rectangle. Now even if waypoints are inside view port bounds (so that outer.contains(inner) condition is satisfied) and zoom is at some maximum level so that my waypoint inner rectangle is not exactly fitting into view port bounds, JxMapViewer does nothing. – user613114 Jun 09 '12 at 13:50
  • In above case JxMapViewer assumes that waypoints are inside view port bounds irrespective of zoom level and stops there. I am trying to fix this issue. But no luck.. – user613114 Jun 09 '12 at 13:56
  • I know from experience there are some unfixed bugs in the JXMapViewer component. Have you tried downloading the source and stepping through it with a debugger (in Eclipse for example) to see why it doesn't do what you expect it to do ? – Jasper Siepkes Jun 09 '12 at 14:39
  • Yes i did that. Please refer the code that i have written in separate answer as i could not post code as a comment. – user613114 Jun 09 '12 at 14:47