通过google所提供的接口,获取要定位地点的经纬度,在页面展示

 获取经纬度代码
  1. package cn.org.map; 
  2.  
  3. import java.io.IOException; 
  4.  
  5. import java.io.InputStreamReader; 
  6.  
  7. import java.io.UnsupportedEncodingException; 
  8.  
  9. import java.net.MalformedURLException; 
  10.  
  11. import java.net.URL; 
  12.  
  13. import java.net.URLConnection; 
  14.  
  15. import javax.servlet.ServletException; 
  16.  
  17. import javax.servlet.http.HttpServlet; 
  18.  
  19. import javax.servlet.http.HttpServletRequest; 
  20.  
  21. import javax.servlet.http.HttpServletResponse; 
  22.  
  23. public class MapServlet extends HttpServlet { 
  24.  
  25.     private static final long serialVersionUID = -6023775612606386229L; 
  26.  
  27.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  28.  
  29.     throws ServletException, IOException { 
  30.  
  31.         this.doPost(request, response); 
  32.  
  33.     } 
  34.  
  35.     /** 
  36.      *  
  37.      * 这里采用的是 
  38.      * csv格式的展现:例如:200,9,34.2334040,108.8691760,按照逗号区分。这里要提取的是后面2个值。一个表示"经度",另外一个表示"纬度" 
  39.      *  
  40.      * 这里在地图上面显示的是一个 位置 
  41.      *  
  42.      */ 
  43.  
  44.     public void doPost(HttpServletRequest request, HttpServletResponse response) 
  45.  
  46.     throws ServletException, IOException { 
  47.  
  48.         String addressOrPoint = "株洲"
  49.         System.out.println("--addressOrPoint--:" + addressOrPoint); 
  50.  
  51.         if (addressOrPoint != null && !"".equals(addressOrPoint)) { 
  52.  
  53.             String data = parseAddressAndPoint(addressOrPoint, "csv""abcdefg");// 获得的结果 
  54.  
  55.             if (data != null) { 
  56.  
  57.                 String[] strArr = data.split(","); 
  58.  
  59.                 request.setAttribute("xxx", strArr[2]);// 经度 
  60.  
  61.                 request.setAttribute("yyy", strArr[3]);// 纬度 
  62.  
  63.             } 
  64.  
  65.         } 
  66.  
  67.         request.getRequestDispatcher("/show.jsp").forward(request, response); 
  68.  
  69.     } 
  70.  
  71.     public void init() throws ServletException { 
  72.  
  73.     } 
  74.  
  75.     /** 
  76.      *  
  77.      * 解析地址和反解析纠经度 
  78.      *  
  79.      * @param addressOrPoint : 
  80.      *            中英文地址,或者是 纬经度,不是经纬度 
  81.      *  
  82.      * @param outPutType 
  83.      *            :这三个数据类型 : xml、json、csv 注:输出格式建议用JSON或CSV,XML格式有时候不能正常显示,比如 
  84.      *            中国:35.8616600,104.1953970 
  85.      *  
  86.      * @param googleKey : 
  87.      *            如果项目发布在网络上,可能要从GOOGLE申请一个GOOGLE MAP KEY才能正常使用 
  88.      *  
  89.      * 参考:http://code.google.com/intl/zh-CN/apis/maps/documentation/services.html#Geocoding_Object 
  90.      *  
  91.      */ 
  92.  
  93.     public String parseAddressAndPoint(String addressOrPoint, 
  94.             String outPutType, String googleKey) { 
  95.  
  96.         try { 
  97.  
  98.             // http://ditu.google.cn 而不用 http;//map.google.com 原因是不能正常解析中文 
  99.  
  100.             String url = "http://ditu.google.cn/maps/geo?output=" + outPutType 
  101.                     + "&q=" 
  102.                     + java.net.URLEncoder.encode(addressOrPoint, "UTF-8"
  103.                     + "&sensor=false&key=" + googleKey; 
  104.  
  105.             URL myUrl = new URL(url); 
  106.  
  107.             URLConnection httpConn = myUrl.openConnection(); 
  108.  
  109.             InputStreamReader isr = new InputStreamReader(httpConn 
  110.                     .getInputStream(), "UTF-8");// 读取数据,并设置数据编码 
  111.  
  112.             int responseInt = isr.read(); 
  113.  
  114.             StringBuffer sb = new StringBuffer(); 
  115.  
  116.             while (responseInt != -1) { 
  117.  
  118.                 sb.append((char) responseInt); 
  119.  
  120.                 responseInt = isr.read(); 
  121.  
  122.             } 
  123.  
  124.             String str = sb.toString(); 
  125.  
  126.             System.out.print(str); 
  127.  
  128.             return str; 
  129.  
  130.         } catch (UnsupportedEncodingException e) { 
  131.  
  132.             e.printStackTrace(); 
  133.  
  134.             return null
  135.  
  136.         } catch (MalformedURLException e) { 
  137.  
  138.             e.printStackTrace(); 
  139.  
  140.             return null
  141.  
  142.         } catch (IOException e) { 
  143.  
  144.             e.printStackTrace(); 
  145.  
  146.             return null
  147.  
  148.         } 
  149.  
  150.     } 
  151.  


页面展示方式

GMap2 中文文档:http://www.codechina.org/doc/google/gmapapi/ 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  2.  
  3.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml"> 
  6.  
  7.   <head> 
  8.  
  9.     <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
  10.  
  11.     <title>Google Maps JavaScript API Example</title> 
  12.  
  13.     <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg" 
  14.  
  15.             type="text/javascript"></script> 
  16.  
  17.     <script type="text/javascript"> 
  18.  
  19.     //<![CDATA[ 
  20.  
  21.  
  22.     function load() { 
  23.  
  24.       if (GBrowserIsCompatible()) { 
  25.  
  26.          var map = new GMap2(document.getElementById("map")); 
  27.           
  28.          //设置为卫星地图,默认平面地图 
  29.          map.setMapType(G_SATELLITE_MAP); 
  30.            
  31.          //启用使用鼠标滚轮缩放。注意:默认情况下禁用滚轮缩放。 
  32.          map.enableScrollWheelZoom(true); 
  33.           
  34.         //var center = new GLatLng(34.2351770, 108.8923490); 
  35.  
  36.          var center = new GLatLng(${xxx}, ${yyy}); 
  37.  
  38.          map.setCenter(center, 13); 
  39.  
  40.          //GMarker定义显示标记,draggable: false 表示标记不允许拖动 
  41.  
  42.          var marker = new GMarker(center, {draggable: false}); 
  43.  
  44.          map.addOverlay(marker); 
  45.  
  46.       } 
  47.  
  48.     } 
  49.  
  50.  
  51.     //]]> 
  52.  
  53.     </script> 
  54.  
  55.   </head> 
  56.  
  57.   <body onload="load()" onunload="GUnload()"> 
  58.  
  59.     <div id="map" style="width: 800px; height: 600px"></div> 
  60. <br/> 
  61.     <input type="button" value="back" onclick="histroy.back(-1);"></input> 
  62.   </body> 
  63.  
  64. </html>