高德GCJ-02坐标转换

高德GCJ-02坐标转换

sf2gis@163.com

2015年7月23日

 

1 高德:WGS84-》GCJ-02

高德使用GCJ-02坐标系统,所以可以直接使用其坐标转换API。其Web服务使用无限制,注册后可以批量转换。

1.1 HTTP坐标转换

注册key之后,使用http请求。

http://restapi.amap.com/v3/assistant/coordinate/convert?locations=116.481499,39.990475&coordsys=gps&output=xml&key=c67d73f88613f4f574df092a93602c43

返回结果

参考:

http://lbsbbs.amap.com/forum.php?mod=viewthread&tid=724&highlight=%E5%9D%90%E6%A0%87%E8%BD%AC%E6%8D%A2

1.2 后台坐标转换:使用servlet调用高德转换的http服务。

转换文档参见:http://lbsbbs.amap.com/forum.php?mod=viewthread&tid=724&extra=&page=9

高德提供的服务是以XML的方式返回。所以在使用时需要在后台调用服务,并将返回的XML结果转换为需要格式。

1.2.1示例:将WGS84坐标转换为GCJ-02坐标。

package com.thbd;

 

importjava.io.BufferedReader;

import java.io.IOException;

importjava.io.InputStreamReader;

import java.io.PrintWriter;

 

importjavax.servlet.ServletConfig;

import javax.servlet.ServletException;

importjavax.servlet.annotation.WebInitParam;

importjavax.servlet.annotation.WebServlet;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

import java.net.URL;

importjava.net.URLConnection;

import java.util.List;

import java.util.Map;

 

/**

 * Servlet implementation class GCJTranlate

 */

@WebServlet(

           urlPatterns = { "/GCJTranlate" },

           initParams = {

                      @WebInitParam(name = "lng", value ="0", description = "longitude"),

                      @WebInitParam(name = "lat", value ="0", description = "latitude")

           })

public class GCJTranlateextends HttpServlet {

      private static final long serialVersionUID = 1L;

 

    /**

     *Default constructor.

     */

    public GCJTranlate() {

        // TODO Auto-generated constructor stub

    }

 

      /**

       * @seeServlet#init(ServletConfig)

       */

      public void init(ServletConfig config) throws ServletException{

           // TODO Auto-generated method stub

      }

 

      /**

       * @seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

       */

      protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

           // TODO Auto-generated method stub

           //create url

           String lng=request.getParameter("lng");

           String lat=request.getParameter("lat");

           System.out.println(lng+","+lat);      

           StringstrUrl="http://restapi.amap.com/v3/assistant/coordinate/convert?locations="+lng+","+lat+"&coordsys=gps&output=xml&key=c67d73f88613f4f574df092a93602c43";

 

           //get translate

           MyHttpRequest req=new MyHttpRequest();

           String strResult=req.requestHttpGet(strUrl);

          

           //create output

           response.setContentType("text/html");

           PrintWriter pw=response.getWriter();

           pw.println("<h1>Hello,I amServlet.</h1>");

           pw.println(strResult);

      }

 

      /**

       * @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

       */

      protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

           // TODO Auto-generated method stub

           doGet(request,response);

      }

}

 

/**

 * @author sf2gis@163.com

 *

 */

class MyHttpRequest{

            public StringrequestHttpGet(String strUrl){

           String strResult="";

           try {

                 //send post request

                 URL url=new URL(strUrl);

                 HttpURLConnection conn=(HttpURLConnection)url.openConnection();

                 conn.connect();

                 Map<String,List<String>>mapHeader=conn.getHeaderFields();

                 for(String key:mapHeader.keySet()){

                      System.out.println(key+"="+mapHeader.get(key));

                 }

                

                 //read response

                 if(conn.getResponseCode()==200){

                      //read response

                      BufferedReader in = new BufferedReader(newInputStreamReader(conn.getInputStream()));

                      String strLine=null;

                      while((strLine=in.readLine())!=null){

                            strResult+=strLine;

                      }

                      System.out.println("response="+strResult);

                 }

                

           } catch (IOException e) {

                 // TODO Auto-generated catch block

                 e.printStackTrace();

           }

           return strResult;    

      }

}

 

 

1.2.2示例:将WGS84坐标转换为GCJ-02坐标,并转换为需要的格式。

package com.thbd;

 

importjava.io.BufferedReader;

import java.io.IOException;

importjava.io.InputStreamReader;

import java.io.PrintWriter;

 

importjavax.servlet.ServletConfig;

importjavax.servlet.ServletException;

importjavax.servlet.annotation.WebInitParam;

importjavax.servlet.annotation.WebServlet;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

importjava.net.HttpURLConnection;

import java.net.URL;

import java.net.URLConnection;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

 

importcom.thoughtworks.xstream.*;

 

/**

 * Servlet implementation class GCJTranlate

 */

@WebServlet(

           urlPatterns = { "/GCJTranlate" },

           initParams = {

                      @WebInitParam(name = "lng", value ="0", description = "longitude"),

                      @WebInitParam(name = "lat", value ="0", description = "latitude")

           })

public class GCJTranlateextends HttpServlet {

      private static final long serialVersionUID = 1L;

 

    /**

     *Default constructor.

     */

    public GCJTranlate() {

        // TODO Auto-generated constructor stub

    }

 

      /**

       * @seeServlet#init(ServletConfig)

       */

      public void init(ServletConfig config) throws ServletException{

           // TODO Auto-generated method stub

      }

 

      /**

       * @seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

       */

      protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

           // TODO Auto-generated method stub

           //create url

           String lng=request.getParameter("lng");

           String lat=request.getParameter("lat");

           System.out.println(lng+","+lat);      

           StringstrUrl="http://restapi.amap.com/v3/assistant/coordinate/convert?locations="+lng+","+lat+"&coordsys=gps&output=xml&key=c67d73f88613f4f574df092a93602c43";

 

           //get translate

           MyHttpRequest req=new MyHttpRequest();

           StringstrResult=req.requestHttpGet(strUrl);//req.requestGet(strUrl);

           GCJPointTranslator pt=new GCJPointTranslator(strResult);

           String[] ptArray=pt.getPoint();

          

           //create output

           response.setContentType("text/html");

           PrintWriter pw=response.getWriter();

           pw.println("<h1>Hello,I amServlet.</h1>");

           pw.println(Arrays.toString(ptArray));

      }

 

      /**

       * @seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

       */

      protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

           // TODO Auto-generated method stub

           doGet(request,response);

      }

}

 

/**

 * @author sf2gis@163.com

 *

 */

class MyHttpRequest{

      public String requestGet(String strUrl){

           String strResult="";

           try {

                 //send post request

                 URL url=new URL(strUrl);

                 URLConnection conn=url.openConnection();

                 conn.connect();

                 Map<String,List<String>>mapHeader=conn.getHeaderFields();

                 for(String key:mapHeader.keySet()){

                      System.out.println(key+"="+mapHeader.get(key));

                 }

                

                 //read response

                 BufferedReader in = new BufferedReader(newInputStreamReader(conn.getInputStream()));

                 StringstrLine=null;

                 while((strLine=in.readLine())!=null){

                      strResult+=strLine;

                 }

                 System.out.println("response="+strResult);

                

                 in.close();

                

           } catch (IOException e) {

                 // TODO Auto-generated catch block

                 e.printStackTrace();

           }

           return strResult;    

      }

     

      public String requestHttpGet(String strUrl){

           String strResult="";

           try {

                 //send post request

                 URL url=new URL(strUrl);

                 HttpURLConnection conn=(HttpURLConnection)url.openConnection();

                 conn.connect();

                 Map<String,List<String>>mapHeader=conn.getHeaderFields();

                 for(String key:mapHeader.keySet()){

                      System.out.println(key+"="+mapHeader.get(key));

                 }

                

                 //read response

                 if(conn.getResponseCode()==200){

                      //read response

                      BufferedReader in = new BufferedReader(newInputStreamReader(conn.getInputStream()));

                      String strLine=null;

                      while((strLine=in.readLine())!=null){

                            strResult+=strLine;

                      }

                      System.out.println("response="+strResult);

                 }

                

           } catch (IOException e) {

                 // TODO Auto-generated catch block

                 e.printStackTrace();

           }

           return strResult;    

      }

}

 

class GCJPointTranslator{

      private String m_strResponse;

     

      /**

       * @return thestrResponse

       */

      public String getResponse() {

           return m_strResponse;

      }

 

      /**

       * @param strResponse thestrResponse to set

       */

      public void setResponse(String strResponse) {

           this.m_strResponse = strResponse;

      }

 

      public GCJPointTranslator(String strResponse) {

           // TODO Auto-generated constructor stub

           setResponse(strResponse);

 

      }

     

      public String[] getPoint(){

           XStream xs=new XStream();

           //map

           xs.alias("response", GCJPoint.class);

           //xml to pojo

           GCJPoint pt2=(GCJPoint)xs.fromXML(getResponse());

           System.out.println("fromXML=status:"+pt2.status+",info:"+pt2.info+",locations:"+pt2.locations);

           String[] pt=pt2.locations.split(",");

           return pt;

      }

}

 

class GCJPoint{

      public int status;

      public String info;

      public String locations;

     

}

阅读更多
个人分类: WEB
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭