百度地图Geocoding API坐标反转

[size=x-large][size=medium][size=x-small]package com.cjjsys.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.springframework.core.io.InputStreamResource;

import com.cjjsys.common.PropertiesUtils;
public class BaiduUtil {
public static final String KEY_1 = PropertiesUtils.getInstance().getValue("ak"); ;

// String key2 = "8ec6cd98bd56554407a207d597c0f3e5";
// String key3 = "be025dc280e1e3f7ffb95fe42a01fab2";
// String key4 = "327db7009617d6806b9c38e819ea06ac";

/**
* 返回输入地址的经纬度坐标
* key lng(经度),lat(纬度)
*/
public String getGeocoderLatitude(String lat,String lng){
System.out.println(lat+",lng:"+lng);
BufferedReader in = null;
try {
//将地址转换成utf-8的16进制
//address = URLEncoder.encode(address, "UTF-8");
// 如果有代理,要设置代理,没代理可注释
// System.setProperty("http.proxyHost","192.168.1.188");
// System.setProperty("http.proxyPort","3128");
//http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse&location=39.983424,116.322987&output=json&pois=1
String uri="http://api.map.baidu.com/geocoder/v2/?ak="+KEY_1+"&callback=renderReverse&" +
"location="+lat+","+lng+"&output=json&pois=0";
//"http://api.map.baidu.com/geocoder?callback=renderReverse&location="+ address +"&output=json&pois=0&key="+ KEY_1
URL tirc = new URL(uri);
HttpURLConnection conn = (HttpURLConnection)tirc.openConnection();
conn.setRequestProperty("Content-Type", "text/html;charset=utf-8");
conn.setDoOutput(true);
//尝试链接5次,如果正确的话就跳出循环没如果连接超时就重新连接
for(int i = 0; i < 5; i ++){
conn.connect();
if(conn.getResponseCode() == 200)
break;
else if(conn.getResponseCode() == 408)
Thread.sleep(5);
};
System.out.println(conn.getResponseCode());
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String res;
StringBuilder sb = new StringBuilder("");
while((res = in.readLine())!=null){
sb.append(res.trim());

}
String str = sb.toString();
System.out.println(str);
Map<String,String> map = null;
if(str!=null&&str.split("formatted_address\"\\:\"")[1]!=null&& str.split("formatted_address\"\\:\"")[1].split("\"\\,")[0]!=null){
System.out.println(str.split("formatted_address\"\\:\"")[1].split("\"\\,")[0]);
return str.split("formatted_address\"\\:\"")[1].split("\"\\,")[0];
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(null!=in){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void main(String args[]){
BaiduUtil baiduUtil= new BaiduUtil();
//43.90637886442,lng:125.30130752475
String str = baiduUtil.getGeocoderLatitude("43.90637886442","125.30130752475");

}
}


[/size][/size][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值