首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

调用googleMap的一个小小施用

2012-10-24 
调用googleMap的一个小小应用google API中文同步文档:http://www.codechina.org/doc/google/gmapapi/?该例

调用googleMap的一个小小应用

google API中文同步文档:http://www.codechina.org/doc/google/gmapapi/

?

该例子是建立在网上 前辈的功劳的基础之上的:

?

通过一个servlet来解析,得到要查询地址的经度和纬度:

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 = request.getParameter("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("index.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;
??}
?}
}

welcome页面:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <title>My JSP 'welcome.jsp' starting page</title>

? </head>
?
? <body>
?? <form action="MapServlet.do" method="post">
???? <table align="center" width="400" border="1">
???? <tr><td align="right">地点</td><td align="left"><input name="addressOrPoint"></td></tr>
??????? <tr><td colspan="2" align="center"><input type="submit" value="提交"></td></tr>
???? </table>
?? </form>
? </body>
</html>

?

?

转向页面:

<!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&amp;v=2&amp;key=abcdefg"
??????????? type="text/javascript"></script>
??? <script type="text/javascript">
??? //<![CDATA[

??? function load() {
????? if (GBrowserIsCompatible()) {
???????
???????? var map = new GMap2(document.getElementById("map"));
??????? //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>
? </body>
</html>

?

热点排行