通过location获取address

1. request url: 1) or 2)

    1). https://ditu.google.cn/maps/api/geocode/json?latlng=xxxx,xxxx&sensor=false

    2). http://ditu.google.cn/maps/api/geocode/json?latlng=xxxx,xxxx&sensor=false

2.response: 1) or 2)

    1).

res = 
{
    "results" :
     [      
        {         
            "address_components" :
            [            
                {    "long_name" : "588",     "short_name" : "588",    "types" : [ "street_number" ] },            
                {    "long_name" : "road", "short_name" : "xxx", "types" : [ "route" ] },            
                {    "long_name" : "county", "short_name" : "xxx", "types" : [ "political", "sublocality", "sublocality_level_1" ] },            
                {    "long_name" : "city", "short_name" : "xxx", "types" : [ "locality", "political" ] },            
                {    "long_name" : "province", "short_name" : "xxx", "types" : [ "administrative_area_level_1", "political" ] },            
                {    "long_name" : "中国",   "short_name" : "CN",     "types" : [ "country", "political" ] },                    
                {   "long_name" : "xxxxx", "short_name" : "315500", "types" : [ "postal_code" ]  }         
            ],         
            "formatted_address" : "中国xx省xx市xx县xx路588号 邮政编码: xxxxx",                
            ......   
        },      
        ......
    ]
    ......
    "status" : "OK"
}

    2).

res = 
{   
	"error_message" : "You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console: https://console.developers.google.com/apis/credentials?project=_",   
	"results" : [],   
	"status" : "OVER_QUERY_LIMIT"
}

3. code

private void sendRequestWithHttpUrlConn(final double lat, final double lng) {
    //子线程中进行联网请求数据
    new Thread(new Runnable() {
        @Override
        public void run() {
            String request = "http://ditu.google.cn/maps/api/geocode/json?latlng=";
            request += lat+","+lng+"&sensor=false";
            debug("request = " + request);

            HttpURLConnection conn = null;
            BufferedReader reader = null;
            try {
                URL url = new URL(request);
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(8000);
                conn.setReadTimeout(8000);
                InputStream is = conn.getInputStream();

                //处理获取的输入流
                reader = new BufferedReader(new InputStreamReader(is));
                String line;
                StringBuilder response = new StringBuilder();

                while ((line = reader.readLine()) != null){
                    response.append(line);
                }
                handleResponse(response.toString());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != reader) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (conn != null){
                    conn.disconnect();
                }
            }
        }
    }).start();
}   
private void handleResponse(String res) {
    debug("handleResponse(), res = " + res);
    try {
        JSONObject resJsonObject = new JSONObject(res);
        String status = resJsonObject.getString("status");
        Log.e(TAG, "status = " + status);

		if (status.equals("OK")) {
	        JSONArray resultsJsonArray = resJsonObject.getJSONArray("results");
	        //Log.e(TAG, "jsonArray = " + jsonArray.toString());
	        if (resultsJsonArray.length() > 0){
	            JSONObject jsonObject = resultsJsonArray.getJSONObject(0);
	            debug("jsonObject = " + jsonObject.toString());

	            mAddress = jsonObject.getString("formatted_address");		            
	        }
		} else if(status.equals("OVER_QUERY_LIMIT")){
			mAddress = "Request data failed, please try again!";
		}
		debug("mAddress : " + mAddress);
		Intent intent = new Intent("com.xxx.location.ACTION");
		intent.setPackage("com.xxx.xxx");//intent must be set package's name in android 80go
		intent.putExtra("ADDRESS", mAddress);
		mContext.sendBroadcast(intent);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

4. 参考:https://www.cnblogs.com/dongling/p/5773154.html

LocationManager的使用    

猜你喜欢

转载自blog.csdn.net/lyl0530/article/details/80849132