Java根据地理位置获取经纬度(调⽤百度地图API)
需求描述:根据地址,得到经纬度,并批量更新⾄数据库。
详细调⽤⽂档参考:
说明:百度地图api个⼈认证AK,⽇配额限制6000个。使⽤达到上限需要第⼆天再⽤,或者更换AK(即密钥)。
运⾏效果如图:
解决⽅式:后附测试Demo
调⽤http接⼝⽅式,若出现如下相似的错误: {"status":200,"message":"APP不存在,AK有误请检查再重试"},
错误详情可查看官⽅⽂档:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.MalformedURLException;
import java.URL;
import java.URLConnection;
DecimalFormat;
import net.sf.json.JSONObject;
/**
* @ClassName: EntCoordSyncJob
* @Description: TODO(这⾥⽤⼀句话描述这个类的作⽤)
*
*/
public class EntCoordSyncJob {
static String AK = "此处需要填写认证AK"; // 百度地图密钥
public static void main(String[] args) {
String dom = "北京王府井"; 
String coordinate = getCoordinate(dom);
System.out.println("'" + dom + "'的经纬度为:" + coordinate);
/
/ println(">#同步坐标已达到⽇配额6000限制,请明天再试!>");
}
// 调⽤百度地图API根据地址,获取坐标
public static String getCoordinate(String address) {
if (address != null && !"".equals(address)) {
address = placeAll("\\s*", "").replace("#", "栋");
String url = "api.map.baidu/geocoder/v2/?address=" + address + "&output=json&ak=" + AK;
String json = loadJSON(url);
if (json != null && !"".equals(json)) {
JSONObject obj = JSONObject.fromObject(json);
if ("0".String("status"))) {
double lng = JSONObject("result").getJSONObject("location").getDouble("lng"); // 经度
double lat = JSONObject("result").getJSONObject("location").getDouble("lat"); // 纬度
DecimalFormat df = new DecimalFormat("#.>#");
return df.format(lng) + "," + df.format(lat);
}
}
}
return null;
}
public static String loadJSON(String url) {
StringBuilder json = new StringBuilder();
try {
URL oracle = new URL(url);
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {} catch (IOException e) {}
String();
}
// 来⾃stackoverflow的MD5计算⽅法,调⽤了MessageDigest库函数,并把byte数组结果转换成16进制
/*
* public String MD5(String md5) { try { java.security.MessageDigest md = java.security.MessageDigest .getInstance("MD5"); byte[] array = md.Bytes()); StringBuffer sb = new StringBuffer(); for (int i = 0; i < array.length; ++i) { s      * } return null; }
*/
}
关于jar包 net.sf.json.JSONObject:
此Demo为maven项⽬,在l⽂件中,添加下⽅配置信息即可使⽤JSON。
replaceall()
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!-- json-lib还需要以下依赖包 -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
maven配置的l⽂件

发表评论