gpt4 book ai didi

java - 将 html 响应图像放入 JFrame 中

转载 作者:行者123 更新时间:2023-12-02 04:30:05 26 4
gpt4 key购买 nike

我有以下 html,我想用它来显示谷歌街景。 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

但是,如果您只想显示图像,您可以执行以下操作...

StreetView

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

关于java - 将 html 响应图像放入 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31577731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com