需求描述:根据地址,得到经纬度,并批量更新至数据库。
详细文档参考:百度地图开放平台
说明:百度地图api个人认证AK,日配额限制6000个。使用达到上限需要第二天再用,或者更换AK(即密钥)。
运行效果如图:
后附Demo。其余说明待续……今天先记录一下。
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.text.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); // System.err.println("######同步坐标已达到日配额6000限制,请明天再试!#####"); } // 调用百度地图API根据地址,获取坐标 public static String getCoordinate(String address) { if (address != null && !"".equals(address)) { address = address.replaceAll("\\s*", "").replace("#", "栋"); String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&output=json&ak=" + AK; String json = loadJSON(url); if (json != null && !"".equals(json)) { JSONObject obj = JSONObject.fromObject(json); if ("0".equals(obj.getString("status"))) { double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng"); // 经度 double lat = obj.getJSONObject("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 InputStreamReader(yc.getInputStream(), "UTF-8")); String inputLine = null; while ((inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) {} catch (IOException e) {} return json.toString(); } // 来自*的MD5计算方法,调用了MessageDigest库函数,并把byte数组结果转换成16进制 /* * public String MD5(String md5) { try { java.security.MessageDigest md = java.security.MessageDigest .getInstance("MD5"); byte[] array = md.digest(md5.getBytes()); StringBuffer sb = new StringBuffer(); for (int i = 0; i < array.length; ++i) { sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100) .substring(1, 3)); } return sb.toString(); } catch (java.security.NoSuchAlgorithmException e) { * } return null; } */ }
关于jar包 net.sf.json.JSONObject:
此Demo为maven项目,在pom.xml文件中,添加下方配置信息即可。
<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>