63286e0995b10e0fe6462162a143479d189e9f9c.svn-base 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package cn.com.goldenwater.dcproj.utils;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. public class AMapUtil {
  10. /**
  11. * 根据详细地址获取经纬度
  12. *
  13. * @param address 详细地址
  14. * @return
  15. */
  16. public static String geocode(String address) {
  17. String result = "";
  18. String url = "https://203.119.244.220/v3/geocode/geo?key=e460caaf3dd5cf2283c4ef1fad6d7d1f&output=JSON";
  19. if (address != null && !"1".equals(address) && !"".equals(address)) {
  20. address = address.trim();
  21. //江苏省-徐州市-睢宁县-睢城街道办事处-中山社区居委会
  22. address = address.replaceAll(" ", "");
  23. address = address.replaceAll("-", "");
  24. if (address.contains("市辖区")) {
  25. address = address.replaceAll("市辖区", "");
  26. }
  27. if (address.contains("办事处")) {
  28. address = address.replaceAll("办事处", "");
  29. }
  30. if (address.contains("社区居民委员会")) {
  31. address = address.replaceAll("社区居民委员会", "社区");
  32. }
  33. if (address.contains("居民委员会")) {
  34. address = address.replaceAll("居民委员会", "村");
  35. }
  36. if (address.contains("民委员会")) {
  37. address = address.replaceAll("民委员会", "");
  38. }
  39. if (address.contains("行政村")) {
  40. address = address.replaceAll("行政村", "村");
  41. }
  42. if (address.contains("社区居委会")) {
  43. address = address.replaceAll("社区居委会", "社区");
  44. }
  45. if (address.contains("村委会")) {
  46. address = address.replaceAll("村委会", "村");
  47. }
  48. if (address.contains("村村")) {
  49. address = address.replaceAll("村村", "村");
  50. }
  51. url += "&address=" + address;
  52. HttpUtil httpUtil = new HttpUtil();
  53. result = httpUtil.getHttpResponse(url);
  54. }
  55. return result;
  56. }
  57. /**
  58. * 根据经纬度获取详细地址
  59. *
  60. * @param location 经纬度,逗号隔开
  61. * @return
  62. */
  63. public static String regeo(String location) {
  64. String result = "";
  65. String url = "http://api.map.baidu.com/geocoding/v3/geocode/regeo?key=e460caaf3dd5cf2283c4ef1fad6d7d1f&output=JSON";
  66. url += "&location=" + location;
  67. HttpUtil httpUtil = new HttpUtil();
  68. System.out.println("url--" + url);
  69. result = httpUtil.getHttpResponse(url);
  70. System.out.println("result=" + result);
  71. return result;
  72. }
  73. /**
  74. * 根据城市名称查询所在经纬度
  75. * @param addr
  76. * 查询的地址
  77. * @return
  78. * @throws IOException
  79. */
  80. public String[] getCoordinate(String addr) throws IOException {
  81. String lng = null;//经度
  82. String lat = null;//纬度
  83. String address = null;
  84. try {
  85. address = java.net.URLEncoder.encode(addr, "UTF-8");
  86. }catch (UnsupportedEncodingException e1) {
  87. e1.printStackTrace();
  88. }
  89. String key = "NcMnc56RX48MjpsOfP4ZEW5GVHmCCmeg";
  90. String url = String .format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s", address, key);
  91. URL myURL = null;
  92. URLConnection httpsConn = null;
  93. try {
  94. myURL = new URL(url);
  95. } catch (MalformedURLException e) {
  96. e.printStackTrace();
  97. }
  98. InputStreamReader insr = null;
  99. BufferedReader br = null;
  100. try {
  101. httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
  102. if (httpsConn != null) {
  103. insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8");
  104. br = new BufferedReader(insr);
  105. String data = null;
  106. int count = 1;
  107. while((data= br.readLine())!=null){
  108. if(count==5){
  109. lng = (String)data.subSequence(data.indexOf(":")+1, data.indexOf(","));//经度
  110. count++;
  111. }else if(count==6){
  112. lat = data.substring(data.indexOf(":")+1);//纬度
  113. count++;
  114. }else{
  115. count++;
  116. }
  117. }
  118. }
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. } finally {
  122. if(insr!=null){
  123. insr.close();
  124. }
  125. if(br!=null){
  126. br.close();
  127. }
  128. }
  129. return new String[]{lng,lat};
  130. }
  131. public static void main(String[] args) {
  132. try {
  133. String[] coordinate = new AMapUtil().getCoordinate("山东省-滨州市-博兴县-锦秋街道办事处-段家村委会");
  134. System.out.println(coordinate[0]+" "+coordinate[1]);//0:经度 1:纬度
  135. } catch (IOException e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. }