高德地图----经纬度转地理位置,地理位置获取经纬度

首先需要获取高德地图开发者key 在调用高德接口时需要用到


一. 根据地理位置转经纬度
高德开发者中心:http://lbs.amap.com/api/webservice/guide/api/georegeo#geo
参数值:address,city

响应结果的格式可以通过请求参数 output 指定,默认为 JSON 形式

最终需要location 中的经纬度
这里写图片描述

代码方法:

// 将地理位置address 作为参数  通过getLngAndLatByAmap  方法获取经纬度
public static String getLngAndLatByAmap(String address) throws Exception{

        address = address.trim();   
        //设置识别城市    
                String city = SysManager.getCity();
        String url;
        try {
        // String 类型字符串需要设置编码格式
            url = "http://restapi.amap.com/v3/geocode/geo?address="+URLEncoder.encode(address,"UTF-8")+"&output=JSON&key="你自己的key"&city="+URLEncoder.encode(city.trim(),"UTF-8");
            HttpDownloader httpDownloader = new HttpDownloader();
            String result = httpDownloader.download(url);
//判断返回结果是否为空
            if (result==null){
                return "-1";
            }
            net.sf.json.JSONObject obj = net.sf.json.JSONObject.fromObject(result);
//判断状态
            if (obj.get("status").toString().equals("1")) {
//如果没有返回经纬度

                net.sf.json.JSONArray array = obj.getJSONArray("geocodes");

                if(array.size()>0){
                    String str = array.getString(0);
                    JSONObject locationjson = JSONObject.parseObject(str);
                    str = locationjson.getString("location");
                    return str;

                }else{
                    System.out.println("未找到相匹配的经纬度!");              
                    return "-1";

                }
            } else {
                System.out.println("请求错误!");
                return "-1";
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  "-1";
    }

二.根据地理位置获取经纬度
所需参数,location,key,radius(查询POI的半径范围。取值范围:0~3000,单位:米 ),extensions(返回结果控制 )
这里写图片描述

最终需要的具体位置:
这里写图片描述

代码方法:

// 将经纬度getLng, getLat   通过getAmapByLngAndLat方法转换地址
    public static String getAmapByLngAndLat(String getLng,String getLat) throws Exception{
        String url;
        try {
            url = "http://restapi.amap.com/v3/geocode/regeo?output=JSON&location="+getLng+","+getLat+"&key="你自己的key"&radius=0&extensions=base";
            HttpDownloader httpDownloader = new HttpDownloader();
            String result = httpDownloader.download(url);
            if (result==null){
                return "-1";
            }
            //将获取结果转为json 数据
            net.sf.json.JSONObject obj = net.sf.json.JSONObject.fromObject(result);
            if (obj.get("status").toString().equals("1")) {
//              如果没有返回-1

                net.sf.json.JSONObject regeocode = obj.getJSONObject("regeocode");
                if(regeocode.size()>0){
    // 在regeocode中拿到 formatted_address 具体位置             
                    String formatted = regeocode.get("formatted_address").toString();               
                    return formatted;

                }else{
                    System.out.println("未找到相匹配的地址!");               
                    return "-1";

                }
            } else {
                System.out.println("请求错误!");
                return "-1";
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  "-1";
    }

代码:https://download.csdn.net/download/ms___/10368673

猜你喜欢

转载自blog.csdn.net/Ms___/article/details/80053004