| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package cn.com.goldenwater.dcproj.utils;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- public class AMapUtil {
- /**
- * 根据详细地址获取经纬度
- *
- * @param address 详细地址
- * @return
- */
- public static String geocode(String address) {
- String result = "";
- String url = "https://203.119.244.220/v3/geocode/geo?key=e460caaf3dd5cf2283c4ef1fad6d7d1f&output=JSON";
- if (address != null && !"1".equals(address) && !"".equals(address)) {
- address = address.trim();
- //江苏省-徐州市-睢宁县-睢城街道办事处-中山社区居委会
- address = address.replaceAll(" ", "");
- address = address.replaceAll("-", "");
- if (address.contains("市辖区")) {
- address = address.replaceAll("市辖区", "");
- }
- if (address.contains("办事处")) {
- address = address.replaceAll("办事处", "");
- }
- if (address.contains("社区居民委员会")) {
- address = address.replaceAll("社区居民委员会", "社区");
- }
- if (address.contains("居民委员会")) {
- address = address.replaceAll("居民委员会", "村");
- }
- if (address.contains("民委员会")) {
- address = address.replaceAll("民委员会", "");
- }
- if (address.contains("行政村")) {
- address = address.replaceAll("行政村", "村");
- }
- if (address.contains("社区居委会")) {
- address = address.replaceAll("社区居委会", "社区");
- }
- if (address.contains("村委会")) {
- address = address.replaceAll("村委会", "村");
- }
- if (address.contains("村村")) {
- address = address.replaceAll("村村", "村");
- }
- url += "&address=" + address;
- HttpUtil httpUtil = new HttpUtil();
- result = httpUtil.getHttpResponse(url);
- }
- return result;
- }
- /**
- * 根据经纬度获取详细地址
- *
- * @param location 经纬度,逗号隔开
- * @return
- */
- public static String regeo(String location) {
- String result = "";
- String url = "http://api.map.baidu.com/geocoding/v3/geocode/regeo?key=e460caaf3dd5cf2283c4ef1fad6d7d1f&output=JSON";
- url += "&location=" + location;
- HttpUtil httpUtil = new HttpUtil();
- System.out.println("url--" + url);
- result = httpUtil.getHttpResponse(url);
- System.out.println("result=" + result);
- return result;
- }
- /**
- * 根据城市名称查询所在经纬度
- * @param addr
- * 查询的地址
- * @return
- * @throws IOException
- */
- public String[] getCoordinate(String addr) throws IOException {
- String lng = null;//经度
- String lat = null;//纬度
- String address = null;
- try {
- address = java.net.URLEncoder.encode(addr, "UTF-8");
- }catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- }
- String key = "NcMnc56RX48MjpsOfP4ZEW5GVHmCCmeg";
- String url = String .format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s", address, key);
- URL myURL = null;
- URLConnection httpsConn = null;
- try {
- myURL = new URL(url);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- InputStreamReader insr = null;
- BufferedReader br = null;
- try {
- httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
- if (httpsConn != null) {
- insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8");
- br = new BufferedReader(insr);
- String data = null;
- int count = 1;
- while((data= br.readLine())!=null){
- if(count==5){
- lng = (String)data.subSequence(data.indexOf(":")+1, data.indexOf(","));//经度
- count++;
- }else if(count==6){
- lat = data.substring(data.indexOf(":")+1);//纬度
- count++;
- }else{
- count++;
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(insr!=null){
- insr.close();
- }
- if(br!=null){
- br.close();
- }
- }
- return new String[]{lng,lat};
- }
- public static void main(String[] args) {
- try {
- String[] coordinate = new AMapUtil().getCoordinate("山东省-滨州市-博兴县-锦秋街道办事处-段家村委会");
- System.out.println(coordinate[0]+" "+coordinate[1]);//0:经度 1:纬度
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
|