Swing JxMapViewer:关注对象并设置相应的缩放级别 [英] Swing JxMapViewer: focus on a object and set corresponding zoom level

查看:191
本文介绍了Swing JxMapViewer:关注对象并设置相应的缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将JxMapViewer整合到我的swing应用程序中。

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.

我看到我们在 JxMApViewer getViewportBounds c $ c>类。同样,我们没有 setViewportBounds 。在这种情况下,我们可以计算一个包含这两个路点的矩形,并设置它。

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.

任何提示都将被欣赏

我已经修改了现有的 calculateZoomFrom 方法:

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);
}

但它不工作。

推荐答案

我找到了解决方案。解决方案是将缩放级别设置为最低级别,然后调用calculateZoomFrom方法。

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

这篇关于Swing JxMapViewer:关注对象并设置相应的缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆