gps2baidu.jsp 1.1 KB

1234567891011121314151617181920212223242526272829
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ page import="java.net.*"%>
  3. <%@ page import="java.io.*"%>
  4. <%
  5. //该JSP是用于将GPS返回的经纬度信息转发给百度地图API进行真实地址的转换。
  6. String x = request.getParameter("x");
  7. String y = request.getParameter("y");
  8. String callbackName = "cbk_"+Math.round(Math.random() * 10000);
  9. String xyUrl = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + x + "&y=" + y + "&callback=BMap.Convertor." + callbackName;
  10. URL url = new URL(xyUrl);
  11. URLConnection conn = url.openConnection(); //打开连接
  12. conn.connect();
  13. BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
  14. StringBuffer content = new StringBuffer();
  15. String temp = "";
  16. while ((temp = in.readLine()) != null) {
  17. content.append(temp);
  18. }
  19. String text = content.toString();
  20. text = text.substring(text.indexOf("(")+1, text.indexOf(")"));
  21. response.setCharacterEncoding("utf-8");
  22. response.setContentType("text/json");
  23. response.getWriter().print("["+text+"]");
  24. %>