文章标签 ‘百度地图’
201112 月22

JAVA源码:将GPS、google地图经纬度坐标转换为百度地图坐标

package smsService;

import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import org.json.me.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import smsService.cfg.Globals;
import smsService.sms.StringUtil;
import smsService.util.JWD;
import smsService.util.SecBase64;

/**
Wizzer.cn
 */
public class baidu {
    public static void main(String args[]) {
        baidu b = new baidu();
        b.getData("117.3094928", "31.875676");
        System.out.println("");
    }

    public static JWD getData(String jd, String wd) {
        JSONObject holder = new JSONObject();
        String wizzer = "";
        JWD jwd=null;
        try {
            BasicHttpParams httpParameters = new BasicHttpParams();
            // Set the default socket timeout (SO_TIMEOUT)
            HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
            // in milliseconds which is the timeout for waiting for data.
            HttpConnectionParams.setSoTimeout(httpParameters, 15000);

            DefaultHttpClient client = new DefaultHttpClient(httpParameters);
            HttpClientParams.setCookiePolicy(client.getParams(), CookiePolicy.NETSCAPE);//CookiePolicy.BROWSER_COMPATIBILITY);
            String type="0";//详见百度坐标转换API文档示例
            type=StringUtil.null2String(Globals.SYS_COM_CONFIG.get("sys.baidu.type"));
            HttpGet get = new HttpGet("http://api.map.baidu.com/ag/coord/convert?from="+type +"&to=4&x=" + jd + "&y=" + wd + "&callback=wizzer");
            HttpResponse resp = client.execute(get);
            HttpEntity entity = resp.getEntity();
            BufferedReader br = new BufferedReader(new InputStreamReader(entity
                    .getContent(), "UTF-8"));
            StringBuffer sb = new StringBuffer();
            String result = br.readLine();
            while (result != null) {
                sb.append(result);
                result = br.readLine();
            }

            String res = StringUtil.null2String(sb.toString());
            if (res.indexOf("(") > 0 && res.indexOf(")") > 0) {
                String str = res.substring(res.indexOf("(") + 1, res.indexOf(")"));
                String err = res.substring(res.indexOf("error") + 7, res.indexOf("error") + 8);

                if ("0".equals(err)) {
                    jwd=new JWD();
                    JSONObject js = new JSONObject(str);
                    String x = new String(SecBase64.decode(js.getString("x").getBytes()));
                    String y = new String(SecBase64.decode(js.getString("y").getBytes()));
                    jwd.setX(x);
                    jwd.setY(y);
                }
            }
            return jwd;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}