java通过传送地址获取坐标

Posted 一念花开满天下

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java通过传送地址获取坐标相关的知识,希望对你有一定的参考价值。

package com.action;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class test {

    /**
     * @Author liuchao email:[email protected]
     * @data 2017-5-11
     * @version 1.0.1
     * @param args
     */
    
     public static String get(String urlAll,String charset){
           BufferedReader reader = null;
           String result = null;
           StringBuffer sbf = new StringBuffer();
           String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/29.0.1547.66 Safari/537.36";//模拟浏览器
           try {
               URL url = new URL(urlAll);
               HttpURLConnection connection = (HttpURLConnection)url.openConnection();
               connection.setRequestMethod("GET");
               connection.setReadTimeout(30000);
               connection.setConnectTimeout(30000);
               connection.setRequestProperty("User-agent",userAgent);
               connection.connect();
               InputStream is = connection.getInputStream();
               reader = new BufferedReader(new InputStreamReader(
                        is, charset));
                String strRead = null;
                while ((strRead = reader.readLine()) != null) {
                    sbf.append(strRead);
                    sbf.append("\r");
                }
                reader.close();
                result = sbf.toString();
               
        } catch (Exception e) {
            e.printStackTrace();
        }
           return result;
       }
     
     public static List<Object> jeixi(String msg){
         List<Object> list=new ArrayList<Object>();
         String json=msg;
         //JSONParser parser=new JSONParser();
         JSONObject obj=JSONObject.fromObject(msg);
         JSONArray result=(JSONArray) obj.get("result");
         JSONObject result1=(JSONObject) result.get(0);
         JSONObject location=(JSONObject) result1.get("location");         
         list.add(obj.get("status"));
         list.add(result1.get("city"));
         list.add(result1.get("district"));
         list.add(result1.get("name"));
         list.add(location.get("lng"));
         list.add(location.get("lat"));
         list.add(obj.get("message"));
         list.add(result1.get("uid"));
         list.add(result1.get("businesss"));
         list.add(result1.get("cityid"));
         list.add(result);
         return list;
     }
    
    public static void main(String[] args){
        // TODO Auto-generated method stub
        //String url="http://api.map.baidu.com/place/v2/suggestion?query=湖北省武汉市洪山区广埠屯&region=湖北省&city_limit=true&output=json&ak=gRhqOOqPOQzvM8nMRnVoQswejvggglqY";
        String url2="http://api.map.baidu.com/place/v2/suggestion?query=上海市浦东新区锦绣申江&region=上海市&city_limit=true&output=json&ak=gRhqOOqPOQzvM8nMRnVoQswejvggglqY";
        String result=test.get(url2, "UTF-8");
        List<Object> result1=test.jeixi(result);
        for(int i=0;i<result1.size();i++){
            System.out.println(result1.get(i));
        }
    }

}

 

以上是关于java通过传送地址获取坐标的主要内容,如果未能解决你的问题,请参考以下文章

MapBox GL JS:从通过代码设置的字符串地址获取经纬度坐标

怎么用程序通过百度地图API批量获取具体地址的经纬度

怎么用程序通过百度地图API批量获取具体地址的经纬度

如何通过经纬度坐标获取附近的地址信息?

java随机生成坐标坐标

Java 获取Excel分页坐标位置(单元格)