zoukankan      html  css  js  c++  java
  • java实现根据高德地图API接口进行地址位置解析,将地址转化为经纬度

    原创文章,转载请注明,欢迎评论和更改。

    1,所需额外ar包,import net.sf.json.JSONObject;

    2,完整源代码代码

    package com.travel.util;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    
    import net.sf.json.JSONObject;
        
    public class AddressLngLatExchange {
        
        public String getLngLat(String address) {
            StringBuffer json = new StringBuffer();
            try {
                URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=7f4ffae4074e8b8e4d147190527a4b72");
                URLConnection yc = u.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 (Exception e) {
                 e.printStackTrace();
             }
            String jsonStr=json.toString();
            JSONObject jsonObject = JSONObject.fromObject(jsonStr);
            
         //判断输入的位置点是否存在
    if(jsonObject.getJSONArray("geocodes").size()>0) return jsonObject.getJSONArray("geocodes").getJSONObject(0).get("location").toString(); else return null; } public static void main(String[] args) { AddressLngLatExchange addressLngLatExchange=new AddressLngLatExchange(); System.out.println(addressLngLatExchange.getLngLat("北京")); } }

    原创文章,转载请注明,欢迎评论和更改。

  • 相关阅读:
    mark::开源绘图工具graphviz
    bzoj1013球形空间产生器sphere 高斯消元(有系统差的写法
    背包专题练习
    仿射加密与S-DES加密算法的实现
    1178:成绩排序
    1177:奇数单增序列
    1176:谁考了第k名
    1311:【例2.5】求逆序对
    1310:【例2.2】车厢重组
    1175:除以13
  • 原文地址:https://www.cnblogs.com/news1997/p/10906414.html
Copyright © 2011-2022 走看看