通过google所提供的接口,获取要定位地点的经纬度,在页面展示
- package cn.org.map;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class MapServlet extends HttpServlet {
- private static final long serialVersionUID = -6023775612606386229L;
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doPost(request, response);
- }
- /**
- *
- * 这里采用的是
- * csv格式的展现:例如:200,9,34.2334040,108.8691760,按照逗号区分。这里要提取的是后面2个值。一个表示"经度",另外一个表示"纬度"
- *
- * 这里在地图上面显示的是一个 位置
- *
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String addressOrPoint = "株洲";
- System.out.println("--addressOrPoint--:" + addressOrPoint);
- if (addressOrPoint != null && !"".equals(addressOrPoint)) {
- String data = parseAddressAndPoint(addressOrPoint, "csv", "abcdefg");// 获得的结果
- if (data != null) {
- String[] strArr = data.split(",");
- request.setAttribute("xxx", strArr[2]);// 经度
- request.setAttribute("yyy", strArr[3]);// 纬度
- }
- }
- request.getRequestDispatcher("/show.jsp").forward(request, response);
- }
- public void init() throws ServletException {
- }
- /**
- *
- * 解析地址和反解析纠经度
- *
- * @param addressOrPoint :
- * 中英文地址,或者是 纬经度,不是经纬度
- *
- * @param outPutType
- * :这三个数据类型 : xml、json、csv 注:输出格式建议用JSON或CSV,XML格式有时候不能正常显示,比如
- * 中国:35.8616600,104.1953970
- *
- * @param googleKey :
- * 如果项目发布在网络上,可能要从GOOGLE申请一个GOOGLE MAP KEY才能正常使用
- *
- * 参考:http://code.google.com/intl/zh-CN/apis/maps/documentation/services.html#Geocoding_Object
- *
- */
- public String parseAddressAndPoint(String addressOrPoint,
- String outPutType, String googleKey) {
- try {
- // http://ditu.google.cn 而不用 http;//map.google.com 原因是不能正常解析中文
- String url = "http://ditu.google.cn/maps/geo?output=" + outPutType
- + "&q="
- + java.net.URLEncoder.encode(addressOrPoint, "UTF-8")
- + "&sensor=false&key=" + googleKey;
- URL myUrl = new URL(url);
- URLConnection httpConn = myUrl.openConnection();
- InputStreamReader isr = new InputStreamReader(httpConn
- .getInputStream(), "UTF-8");// 读取数据,并设置数据编码
- int responseInt = isr.read();
- StringBuffer sb = new StringBuffer();
- while (responseInt != -1) {
- sb.append((char) responseInt);
- responseInt = isr.read();
- }
- String str = sb.toString();
- System.out.print(str);
- return str;
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return null;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- return null;
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- }
页面展示方式GMap2 中文文档:http://www.codechina.org/doc/google/gmapapi/
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
- <title>Google Maps JavaScript API Example</title>
- <script src="http://maps.google.com/maps?file=api&v=2&key=abcdefg"
- type="text/javascript"></script>
- <script type="text/javascript">
- //<![CDATA[
- function load() {
- if (GBrowserIsCompatible()) {
- var map = new GMap2(document.getElementById("map"));
- //设置为卫星地图,默认平面地图
- map.setMapType(G_SATELLITE_MAP);
- //启用使用鼠标滚轮缩放。注意:默认情况下禁用滚轮缩放。
- map.enableScrollWheelZoom(true);
- //var center = new GLatLng(34.2351770, 108.8923490);
- var center = new GLatLng(${xxx}, ${yyy});
- map.setCenter(center, 13);
- //GMarker定义显示标记,draggable: false 表示标记不允许拖动
- var marker = new GMarker(center, {draggable: false});
- map.addOverlay(marker);
- }
- }
- //]]>
- </script>
- </head>
- <body onload="load()" onunload="GUnload()">
- <div id="map" style="width: 800px; height: 600px"></div>
- <br/>
- <input type="button" value="back" onclick="histroy.back(-1);"></input>
- </body>
- </html>