【JAVA】百度地图经纬度纠偏

目前在国内使用电子地图获取到的经纬度都不是真实的经纬度,而是经过一定的算法在真实的经纬度上添加了一个偏移量,且不同的地图有不同的算法。现在告诉大家在java中怎样对百度地图进行纠偏,主要实现将真实的经纬度在百度地图上进行显示,消除偏差。

    一、若需要消偏的经纬度较少,则直接在浏览器中进行即可,百度提供了相应的API接口

  1、API地址:http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=113.240324&y=23.817349
      from=0:代表传入的是真实经纬度
           to=4:代表返回是百度纠偏后,能在百度地图上正确显示出该地址的经纬度

           x:经度         

           y:纬度

           返回数据:{"error":0,"x":"MTEzLjI1MjIyMjUxOTg1","y":"MjMuODIwNjM5MTEyNDgy"}
           返回的数据经过Base64加密,在网上找个在线Base64解密的网站就可以了

   二、若数据量较大,则通过上述方式就不方便了,这里提供Java方法进行批量消偏,代码如下:

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;

public class Remove {
    public static void main(String[] args) {
        try {
            //这里只有一条数据,有多条数据的话可以用循环,然后拼接url字符串
            String url = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=113.540124&y=23.517846";
            JSONObject json = getAllEmployee(url);
            //将经纬度解码后进行打印
            String latitude = decode(json.getString("x"));
            String longitude = decode(json.getString("y"));
            System.out.println("经度为:" + latitude);
            System.out.println("纬度为:" + longitude);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    
    /**
     * Java后台访问url链接,返回JSON格式的数据
     * @return
     */
    public static JSONObject getAllEmployee(String url) {
        try {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>() {
                // 成功调用连接后,对返回数据进行的操作
                public JSONObject handleResponse(final HttpResponse response)
                        throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        // 获得调用成功后 返回的数据
                        HttpEntity entity = response.getEntity();
                        if (null != entity) {
                            String result = EntityUtils.toString(entity);
                            // 根据字符串生成JSON对象
                            JSONObject resultObj = JSONObject.fromObject(result);
                            return resultObj;
                        } else {
                            return null;
                        }
                    } else {
                        throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }
            };
            // 返回的json对象
            JSONObject responseBody = httpclient.execute(httpPost, responseHandler);
            return responseBody;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Base64解码
     * @param str
     * @return
     */
    public static String decode(String str) {
        byte[] bt = null;
        String s= "";
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            bt = decoder.decodeBuffer(str);
            s = new String(bt, "GB2312");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }
}

三、运行上述代码所需的jar包如下:

点击下载



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值