百度地圖API入門——(4)用Jetty搭建一個簡單的多終端可訪問的地圖應用站點...

鍾超發表於2011-12-29

我們這裡以本地區域網為例。


1. 下載Jetty包和Servlet包

jetty-all-7.0.2.v20100331.jar

servlet-api-2.5.jar


2. 編寫地圖網頁程式

參考:http://blog.csdn.net/poechant/article/details/7107579

<html>
	<head>
		<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
		<meta http-equiv="Content-Type" content="text/html; chartset=utf-8" />
		<title>廣州天河公園</title>
		<style type="text/css">
			html {
				height: 100%
			}
			body {
				height:100%;
				margin:0px;
				padding:0px;
			}
			#container {
				height:100%;
			}
		</style>
		<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
	</head>

	<body>
		<div id="container"></div>
		<script type="text/javascript">
			var map = new BMap.Map("container");
			var point = new BMap.Point(113.370, 23.133);
			map.centerAndZoom(point, 15);
			map.addControl(new BMap.NavigationControl());//平移縮放控制元件
			map.addControl(new BMap.ScaleControl());//比例尺控制元件
			map.addControl(new BMap.OverviewMapControl());//縮略地圖控制元件
			map.addControl(new BMap.MapTypeControl());//地圖型別控制元件
			map.setCurrentCity("廣州");
		</script>
	</body>
</html>


具體闡釋,請看博文:http://blog.csdn.net/poechant/article/details/7107579


3. 簡單的Jetty Server


程式同博文:http://blog.csdn.net/poechant/article/details/7068809

具體解釋,請見上述連結,下面是程式原始碼:


package com.sinosuperman.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class JettyServer extends AbstractHandler
{
    public void handle(String target,
                       Request baseRequest,
                       HttpServletRequest request,
                       HttpServletResponse response) 
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        
        File file = new File("web/index.html");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = br.readLine();
        while (line != null) {
        	response.getWriter().println(line);
        	line = br.readLine();
        }
        br.close();
        
        //response.getWriter().println("<h1>Hello World</h1>");
    }
 
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(9001);
        server.setHandler(new JettyServer());
 
        server.start();
        server.join();
    }
}

4. 工程的目錄組織為:



5. 執行並訪問

Jetty Server執行後,在瀏覽器中訪問。如果你的機器的IP地址是192.168.10.4,則可以訪問192.168.10.4:9001。分別通過PC機、手機等終端訪問。


PC終端訪問:



移動終端訪問:


相關文章