通过Panel中显示google地图



这个是显示地图的JPanel,通过BrTabbed执行jsp显示,就像浏览器一样
package com.etsolar.monitor.client.mainpanel.gispanel;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
//Java网页浏览器
import org.jdic.web.BrTabbed;

public class GISPanel extends JPanel { 
	private static final long serialVersionUID = 1L;
	private BrTabbed browser = new BrTabbed();
	public GISPanel(){
		try {
			this.setBorder(BorderFactory.createTitledBorder("GIS Panel"));
			String fileDir = "C:" + System.getProperty("file.separator")+ "etsolar";
			String fileName = fileDir + System.getProperty("file.separator") + "GIS.jsp";
			browser.setURL(fileName);
			this.setLayout(new BorderLayout());
			this.add(new JScrollPane(browser), BorderLayout.CENTER);
			org.jdic.web.BrComponent.DESIGN_MODE = false;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public boolean executeJavaScript(String javaScript)
	{
		boolean result = true;
		return result;
	}
	
	public void refresh()
	{
		browser.refresh();
	}
}


执行的程序,使用XML来保存需要显示的地点的信息(精度和纬度)
import java.awt.BorderLayout;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.jdic.web.BrTabbed;

public class GISTest extends JPanel{

	private BrTabbed browser = new BrTabbed();
	public GISTest(){
		this.setBorder(BorderFactory.createTitledBorder("GIS Panel"));
//		String fileDir = "C:" + System.getProperty("file.separator")+ "etsolar";
		String fileDir = "c://etsolar";
		String fileName = fileDir + System.getProperty("file.separator") + "GIS.jsp";
		browser.setURL(fileName);
		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(browser), BorderLayout.CENTER);
		org.jdic.web.BrComponent.DESIGN_MODE = false;
	}
	
	public void show(){
		String num1Str = "30.657";
		String num2Str = "104.08";
		Document document = DocumentHelper.createDocument();
		Element catalogElement = document.addElement("description");
		catalogElement.addComment("An XML description of a machine");
		catalogElement.addProcessingInstruction("target", "text");
		Element detailElement = catalogElement.addElement("detail");
		detailElement.addAttribute("publisher", "NJUST");
		Element LongitudeElement = detailElement.addElement("Longitude");
		LongitudeElement.setText(num1Str);
		Element LatitudeElement = detailElement.addElement("Latitude");
		LatitudeElement.setText(num2Str);
		String fileDir = "C:" + System.getProperty("file.separator")
				+ "etsolar";
		String fileName = fileDir + System.getProperty("file.separator")
				+ "MAP.xml";
		XMLWriter output;
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		try {
			output = new XMLWriter(new FileWriter(fileName), format);
			output.write(document);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//System.out.println("经纬度信息已经写入XML文件" + "经度: [" + num1Str + "],"					+ "纬度: [" + num2Str + "]");
		this.refresh();
	}
	
	public boolean executeJavaScript(String javaScript)
	{
		boolean result = true;
		return result;
	}
	
	public void refresh()
	{
		browser.refresh();
	}
	
	public static void main(String[] args){
		JFrame frame = new JFrame("GISTest");
		GISPanel gisPanel = new GISPanel();
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				new JPanel(), gisPanel);
		frame.add(splitPane);
		splitPane.updateUI();
		frame.setVisible(true);
		frame.setSize(400, 500);
	}
}


这个只最重要的jsp文件啦
GIS.jsp

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
		<title>Etsolar Google Maps </title>	
		<script src="http://ditu.google.com/maps?file=api&amp;v=2"
            type="text/javascript"></script>
		<script type="text/javascript">
		var Url="C:\\etsolar\\MAP.xml";
		var x;var y;
		var cdsales=new ActiveXObject("Microsoft.XMLDOM"); 
		cdsales.async=true; 
		cdsales.load(Url); 
		function initialize()
		{
			if(cdsales.readyState==4)
			{
				if(cdsales.parseError.errorCode != 0)
				{
					document.write(cdsales.parseError.line);
					document.write(cdsales.parseError.reason);
					document.write(cdsales.parseError.srcText);
				}
				else
				{
					var bi=cdsales.documentElement.selectNodes("//detail");
					if(bi!=null)
					{
							x=bi[0].childNodes[0].text;
							y=bi[0].childNodes[1].text;
					}
				}
			}
			else
			{
				document.write("XML文档未分析完");
			}
			
			if(GBrowserIsCompatible())
			{
				var map = new GMap2(document.getElementById("maps"));
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());
				var blueIcon = new GIcon(G_DEFAULT_ICON); 
				blueIcon.image = "computer20.gif"; 
                 
				markerOptions = { icon:blueIcon }; 

				var point = new GLatLng(x,y);
				var marker = new GMarker(point,markerOptions);
				map.setCenter(point,19);
        map.addOverlay(marker);
				GEvent.addListener(map,"click", function(overlay,point) {        
       	if(point)
				{
						document.getElementById("show_x").value = point.x;
        		document.getElementById("show_y").value = point.y;
				}  
        });
        map.addControl(new GSmallMapControl());   
				map.addControl(new GMapTypeControl());   
			}
		}
	</script>
</head>
	
	<body onLoad="initialize()" onUnload="GUnload()">
		<b><font color="#FF0000">经度:</font></b><input id="show_x" value="0">&nbsp;&nbsp;<b><font color="#FF0000">纬度:</font></b><input id="show_y" value="0">
		<div id="maps" style="width:1000px;height:700px"></div>
	</body>
</html>

你可能感兴趣的:(JavaScript,xml,jsp,swing,Google)