高德地图根据经纬度获取位置

我现在返回的是详细地址,如果需要返回省 市 区 或者你想要的格式只需在返回的结果中进行拆分即可

import java.net.URL;
import net.sf.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URLConnection;

public class LocationUtile {
    
    private static final String key = "这里填写申请的高德地图key";

    public static String getCoordinate(String lng, String lat) throws IOException {

        StringBuilder resultData = new StringBuilder();
        StringBuilder https = new StringBuilder("http://restapi.amap.com/v3/geocode/regeo?key=");
        //经纬度地址
        StringBuilder localhost = new StringBuilder("&location="+lng+","+lat);
        StringBuilder httpsTail = new StringBuilder("&poitype=&radius=&extensions=base&batch=true");
        String url = https.append(key).append(localhost).append(httpsTail).toString();
        //拼接出来的地址
        //System.out.println(https1.append(key).append(localhost1).append(httpsTail).toString());
        // String url ="http://restapi.amap.com/v3/geocode/regeo?key=自己申请的key&location=116.310003,39.991957&poitype=&radius=&extensions=base&batch=true&roadlevel=";
        URL myURL = null;
        URLConnection httpsConn = null;
        try {
            myURL = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStreamReader insr = null;
        BufferedReader br = null;
        try {
            httpsConn = myURL.openConnection();// 不使用代理
            if (httpsConn != null) {
                insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
                br = new BufferedReader(insr);
                String data = null;
                while ((data = br.readLine()) != null) {
                    resultData.append(data);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (insr != null) {
                insr.close();
            }
            if (br != null) {
                br.close();
            }
        }
        if (resultData.toString().indexOf("regeocodes") == 0) {
            return null;
        }
        String str = JSONObject.fromObject(resultData.toString()).getString("regeocodes");
        //城市切割
        String[] strr = str.split("\"formatted_address\":\"");
        //        if (strr.length < 2 && strr.length == 1) {
//            //直辖市
//            String[] sr = str.split("\"province\":\"");
//            String[] srr = sr[1].split("\",\"city");
//            return srr[0];
//        }
        //非直辖市
        String[] strrr = strr[1].split("\",\"addressComponent\":");
        return strrr[0];
    }
//    public static void main(String[] args) {
//        try {
//            //测试使用
//            System.out.println(getCoordinate("107.6493856959542", "35.71521598356201"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

}

你可能感兴趣的:(高德地图)