根据名称将其转换为经纬度坐标

public static String getGeocoderLatitude(String address){
        BufferedReader in = null;
        String lng ="";
        String lat ="";
        try {
            address = URLEncoder.encode(address, "UTF-8");
            URL tirc = new URL("http://api.map.baidu.com/geocoder?address="+ address +"&output=json&key="+"7d9fbeb43e975cd1e9477a7e5d5e192a");
            in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));
            String res;
            StringBuilder sb = new StringBuilder("");
            while((res = in.readLine())!=null){
                sb.append(res.trim());
            }
            String str = sb.toString();

            if(StringUtils.isNotEmpty(str)){
                int lngStart = str.indexOf("lng\":");
                int lngEnd = str.indexOf(",\"lat");
                int latEnd = str.indexOf("},\"precise");
                if(lngStart > 0 && lngEnd > 0 && latEnd > 0){
                    lng = str.substring(lngStart+5, lngEnd);
                    lat = str.substring(lngEnd+7, latEnd);
                    System.out.println("=="+lng);
                    System.out.println("++"+lat);
                }
            }

        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return lng+","+lat;
    }

你可能感兴趣的:(记录)