高德地图获取经纬度工具类

高德地图获取经纬度工具类

MapUtils
public class MapUtils {

    public static String getHttpResponse(String serverUrl) {
        BufferedReader bf = null;
        StringBuffer result = null;
        try {
            //构造一个URI
            URI uri = new URI(serverUrl);
            //根据URI构造一个URL
            URL url = uri.toURL();
            //返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接
            URLConnection connection = url.openConnection();
            //设置请求属性
            connection.setRequestProperty("Content-type", "text/html");
            connection.setRequestProperty("Accept-Charset", "utf-8");
            connection.setRequestProperty("ContentType", "utf-8");
            connection.connect();
            result = new StringBuffer();
            //读取打开的连接的输入流并放入到缓冲区中
            bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            String line;
            while ((line = bf.readLine()) != null) {
                result.append(line);
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bf != null) {
                    bf.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    public static String distance(String origin, String destination) {
        String url = "http://restapi.amap.com/v4/direction/truck?"
                + "&size=" + 4
                + "&origin=" + origin
                + "&destination=" + destination
                + "&key=" + "5ccc58bc0e4447bb60a192ad5146055c";
        String distanceString = getHttpResponse(url);
//JSONObject json = JSONObject.parseObject(distanceString);
        return distanceString;
    }

    /**
     * 高德地图通过地址获取经纬度
     */
    public static Map<String, Double> addressToLngAndLag(String address) {
        Map<String, Double> map = new HashMap<String, Double>();
        //"http://restapi.amap.com/v3/geocode/geo?address=上海市东方明珠&output=JSON&key=xxxxxxxxx";
        String geturl = "http://restapi.amap.com/v3/geocode/geo?key=5ccc58bc0e4447bb60a192ad5146055c&address=" + address;
        String location = "";
        try {
            URL url = new URL(geturl);    // 把字符串转换为URL请求地址
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
            connection.connect();// 连接会话
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接
            com.alibaba.fastjson.JSONObject a = JSON.parseObject(sb.toString());
            JSONArray sddressArr = JSON.parseArray(a.get("geocodes").toString());
            if (sddressArr.size() == 0) return null;
            JSONObject c = JSON.parseObject(sddressArr.get(0).toString());
            location = c.get("location").toString();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败!");
        }
        return stringToMap(location);
    }

    public static Map<String, Double> stringToMap(String LngLat) {
        Map<String, Double> map = new HashMap<String, Double>();
        String[] strArr = LngLat.split("\\,");
        map.put("lng", Double.parseDouble(strArr[0]));
        map.put("lat", Double.parseDouble(strArr[1]));
        return map;
    }


}
测试demo
@Test
    public void e() {
        Map<String, Double> queryString = MapUtils.addressToLngAndLag("平阳居公寓");
        Double lng = queryString.get("lng");//纬度
        Double lat = queryString.get("lat");//精度
        System.out.println(lat);//27.632457
        System.out.println(lng);//120.318786
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值