簡體   English   中英

將html響應圖像放入JFrame

[英]Put html response image in JFrame

我想使用以下HTML來顯示Google Streetview。 HTML已經過測試,可以返回街景,但是我不確定如何設置%% lat %%和%% long %%通配符值,也不確定如何檢索街景並將其放入JFrameJOptionPane HTML如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
    function initialize() {
        var markerPosition = new google.maps.LatLng(%%lat%%, %%lon%%);
        var panoramaOptions = {
            position: markerPosition,
            pov: {
                heading: 165,
                pitch: 0,
                zoom: 1
            }
        };
        var myPano = new google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);
        myPano.setVisible(false);
        new google.maps.Marker({map: myPano, position: markerPosition, title: 'Feature'});

        var changeImage = function(){
            var image = "https://maps.googleapis.com/maps/api/streetview?size=600x300&location=44.414382,11.013988&heading=151.78&pitch=-0.76";
            document.getElementById("pano").innerHTML = "<img src='" + image + "' style='width:100%;height:100%'>"; 
        }
        // add a variable that gets set when the position_changed handler gets fired off
        var positionDidChange = false;
        var newPov = {};
        var listenerHandle = google.maps.event.addListener(myPano, 'position_changed', function () {
            positionDidChange = true;
            google.maps.event.removeListener(listenerHandle);
            newPov.heading = google.maps.geometry.spherical.computeHeading(myPano.getPosition(), markerPosition);
            newPov.pitch = 0;
            newPov.zoom = 1;
            myPano.setPov(newPov); myPano.setVisible(true);
        });

        // add a function that gets fired off to see if the position did change so that the user does not wait forever
        setTimeout(function () {
            if (!positionDidChange) {
                changeImage();
            }
        }, 5000);
    }
</script>
</head>
<body onload="initialize()">
<div id="pano" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0;      text-align: center"> LOADING STREET VIEW...</div>
</body>
</html>

我想從以下方法獲得響應:

    @Override
protected void onMouseDown(MouseButton button,
                           KeyModifier keyModifier,
                           int mouseX,
                           int mouseY) {
    try {
        if (MouseButton.LEFT.equals(button)) {

            FeatureWrapper closestStationOrSpanFw = SelectionUtil.getClosestSpanOrStation(toMapPoint(mouseX, mouseY));
            if (closestStationOrSpanFw != null) {
                IGeometry shape = closestStationOrSpanFw.getIFeature().getShapeCopy();
                IPoint point = null;
                if (shape instanceof IPoint) {
                    point = (IPoint) shape;
                    double lat = point.getY();
                    double lon = point.getX();
                    ....

我認為讓HTML在Swing中運行不會取得很大的成功,因為HTML / Web支持已經過時。 一個解決方案可能是使用Java-FX中的WebView

但是,如果您只想顯示圖像,則可以執行以下操作:

街景

    try {
        URL url = new URL("https://maps.googleapis.com/maps/api/streetview?size=400x400&location=40.720032,-73.988354&fov=90&heading=235&pitch=10");
        try (InputStream is = url.openStream()) {
            BufferedImage img = ImageIO.read(is);
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));
        }
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

現在,我可能很想寫一個采用參數並生成URL

protected static final String BASE_URL = "https://maps.googleapis.com/maps/api/streetview?size=400x400&";

public BufferedImage getStreetView(double lat, double log, double fov, double heading, double pitch) throws IOException {

    StringJoiner sj = new StringJoiner("&");
    sj.add(BASE_URL);
    sj.add("location=" + Double.toString(lat) + "," + Double.toString(log));
    sj.add("fov=" + Double.toString(fov));
    sj.add("heading=" + Double.toString(heading));
    sj.add("pitch=" + Double.toString(pitch));

    BufferedImage img = null;
    URL url = new URL(sj.toString());
    try (InputStream is = url.openStream()) {
        img = ImageIO.read(is);
    }

    return img;
}

或者更好的是某種建造者

然后,您可以使用類似...的名稱來調用它

BufferedImage img = getStreetView(40.720032, -73.988354, 90d, 235d, 10d);
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM