World Wind Java开发之十一——加载热点信息(仿Google Earth)(转)
在GE的图层中有一个照片图层,在浏览时可以看到各地的一些图片,我们称之为热点信息,如下图所示:
再来看下本文的实现效果:
效果是不是很像呢,其实实现这个很简单,参照examples中的Balloons就可以很容易的实现,这里我自己封装了BalloonUtil类便于复用,代码很简单都加了注释就不再一一展开了,直接附上源代码,有看不明白的地方可以留言交流哈。
- /**
- * @Copyright 2014-2020 @��˶
- **/
- package edu.whu.vge.util;
- import gov.nasa.worldwind.avlist.AVKey;
- import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
- import gov.nasa.worldwind.geom.Position;
- import gov.nasa.worldwind.layers.RenderableLayer;
- import gov.nasa.worldwind.render.AbstractBrowserBalloon;
- import gov.nasa.worldwind.render.BalloonAttributes;
- import gov.nasa.worldwind.render.BasicBalloonAttributes;
- import gov.nasa.worldwind.render.GlobeBrowserBalloon;
- import gov.nasa.worldwind.render.PointPlacemark;
- import gov.nasa.worldwind.render.Size;
- import gov.nasa.worldwind.util.Logging;
- import gov.nasa.worldwind.util.WWIO;
- import gov.nasa.worldwindx.examples.util.BalloonController;
- import gov.nasa.worldwindx.examples.util.HotSpotController;
- import java.io.InputStream;
- /**
- *
- * @项目名称:SMartScope
- * @类名称:BalloonsUtil
- * @类描述:
- * @创建人:刘硕
- * @创建时间:2015年2月3日 下午4:56:26
- * @修改备注:
- * @版本:
- */
- public class BalloonsUtil
- {
- private double balloonLat; // 气球纬度
- private double balloonLon; // 气球经度
- private String balloonContentPath; // html文件路径
- private String balloonName; // 气球名称
- protected HotSpotController hotSpotController;
- protected BalloonController balloonController;
- /**
- *
- * 创建一个新的实例 BalloonsUtil.
- *
- * @param balloonLat
- * @param balloonLon
- * @param balloonContentPath
- * @param balloonName
- */
- public BalloonsUtil(double balloonLat, double balloonLon,
- String balloonContentPath, String balloonName)
- {
- super();
- this.balloonLat = balloonLat;
- this.balloonLon = balloonLon;
- this.balloonContentPath = balloonContentPath;
- this.balloonName = balloonName;
- }
- /**
- * s
- *
- * @方法名称: makeBrowserBalloon ;
- * @方法描述: 生成Balloon标记图层 ;
- * @参数 :@param windowGLCanvas
- * @参数 :@return
- * @返回类型: RenderableLayer ;
- * @创建人:刘硕;
- * @创建时间:2015年2月3日 下午5:01:03;
- * @throws
- */
- public RenderableLayer makeBrowserBalloon(WorldWindowGLCanvas windowGLCanvas)
- {
- // ע��balloonController
- this.hotSpotController = new HotSpotController(windowGLCanvas);
- this.balloonController = new BalloonController(windowGLCanvas);
- RenderableLayer layer = new RenderableLayer();
- layer.setName(balloonName);
- String htmlString = null;
- InputStream contentStream = null;
- try
- {
- // 读取html文件内容
- contentStream = WWIO.openFileOrResourceStream(balloonContentPath,
- null);
- htmlString = WWIO.readStreamToString(contentStream, null);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- WWIO.closeStream(contentStream, balloonContentPath);
- }
- if (htmlString == null) htmlString = Logging.getMessage(
- "generic.ExceptionAttemptingToReadFile", balloonContentPath);
- // 创建一个GlobeBrowserBalloon
- Position balloonPosition = Position.fromDegrees(balloonLat, balloonLon);
- AbstractBrowserBalloon balloon = new GlobeBrowserBalloon(htmlString,
- balloonPosition);
- // 设置GlobeBrowserBalloon属性
- BalloonAttributes attrs = new BasicBalloonAttributes();
- attrs.setSize(new Size(Size.NATIVE_DIMENSION, 0d, null,
- Size.NATIVE_DIMENSION, 0d, null));
- balloon.setAttributes(attrs);
- // 将GlobeBrowserBalloon与PointPlacemark关联起来
- PointPlacemark placemark = new PointPlacemark(balloonPosition);
- placemark.setLabelText(balloonName);
- placemark.setValue(AVKey.BALLOON, balloon);
- layer.addRenderable(balloon);
- layer.addRenderable(placemark);
- return layer;
- }
- public double getBalloonLat()
- {
- return balloonLat;
- }
- public void setBalloonLat(double balloonLat)
- {
- this.balloonLat = balloonLat;
- }
- public double getBalloonLon()
- {
- return balloonLon;
- }
- public void setBalloonLon(double balloonLon)
- {
- this.balloonLon = balloonLon;
- }
- public String getBalloonContentPath()
- {
- return balloonContentPath;
- }
- public void setBalloonContentPath(String balloonContentPath)
- {
- this.balloonContentPath = balloonContentPath;
- }
- public String getBalloonName()
- {
- return balloonName;
- }
- public void setBalloonName(String balloonName)
- {
- this.balloonName = balloonName;
- }
- }
调用的时候,只需键入以下代码即可:
- String htmlPath = System.getProperty("user.dir")
- + "\\src\\edu\\whu\\vge\\data\\whu.html";
- BalloonsUtil balloonsUtil = new BalloonsUtil(30.5271,
- 114.3604, htmlPath, "我在这里");
- wwPanel.getWorldWindowGLCanvas()
- .getModel()
- .getLayers()
- .add(balloonsUtil.makeBrowserBalloon(wwPanel
- .getWorldWindowGLCanvas()));
另外,附上whu.html网页的代码以供大家参考。
- <html>
- <head>
- <style type='text/css'>
- html, body {
- margin: 0 0;
- width: 100%;
- height: 100%;
- overflow: hidden
- }
- a:link {
- color: #0000CD;
- text-decoration: underline
- }
- table {
- font-family: verdana, 黑体, sans-serif;
- font-size: 12px;
- color: #4169E1;
- border-width: 1px;
- border-color: #a9c6c9;
- border-collapse: collapse
- }
- th {
- background-color: #4169E1;
- color: #fff
- }
- tr {
- background-color: #d4e3e5
- }
- td {
- border-width: 1px;
- border-style: solid;
- border-color: #a9c6c9
- }
- </style>
- <title></title>
- </head>
- <body>
- <table border='0'>
- <tr>
- <th align='center' colspan='2'>武汉大学</th>
- </tr>
- <tr>
- <td rowspan='7'><a href="http://www.whu.edu.cn/index.htm"> <img
- border='0' alt='' width='300px' height='225px'
- src='http://news.whu.edu.cn/_mediafile/whu_news/2015/02/02/1eq0euop1q.jpg'>
- </a></td>
- </tr>
- </table>
- </body>
- </html>