传入一个中文地址,如何获取其经纬度?
很早之前有这样一个需求,项目里用到了百度地图。要根据地址,得到经纬度,并更新至数据库。
说明:百度地图api个人认证AK,每天有6000个限额。使用达到上限需要第二天再用,或者更换AK(即密钥)。
后附Demo,导入相关jar包,即可运行。其余说明待续……今天先记录一下。
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 coordinate = getCoordinate("北京王府井"); System.out.println(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(); } // 来自stackoverflow的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; } */ }