zoukankan      html  css  js  c++  java
  • Android: How to get Address from geolocation using Geocoder

    Class: HttpRetriever

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    public class HttpRetriever {
        private DefaultHttpClient client = new DefaultHttpClient();
     
        public String retrieve(String url) {
            HttpGet getRequest = new HttpGet(url);
            try {
                HttpResponse getResponse = client.execute(getRequest);
                final int statusCode = getResponse.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    Log.w(getClass().getSimpleName(), "Error " + statusCode
                            + " for URL " + url);
                    return null;
                }
                HttpEntity getResponseEntity = getResponse.getEntity();
     
                if (getResponseEntity != null) {
                    return EntityUtils.toString(getResponseEntity);
                }
            } catch (IOException e) {
                getRequest.abort();
                Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
            }
            return null;
     
        }
    }

    Method: getAddressFromGPSData

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public static final String GOOGLE_GEOCODER = "http://maps.googleapis.com/maps/api/geocode/json?latlng=";
     
    public static String getAddressFromGPSData(double lat, double longi) {
            HttpRetriever agent = new HttpRetriever();
            String request = GOOGLE_GEOCODER + lat + ","
                    + longi + "&sensor=true";
            // Log.d("GeoCoder", request);
            String response = agent.retrieve(request);
            String formattedAddress = "";
            if (response != null) {
                Log.d("GeoCoder", response);
                try {
                    JSONObject parentObject = new JSONObject(response);
                    JSONArray arrayOfAddressResults = parentObject
                            .getJSONArray("results");
                    JSONObject addressItem = arrayOfAddressResults.getJSONObject(0);
                    formattedAddress = addressItem.getString("formatted_address");
                } catch (JSONException e) {
     
                    e.printStackTrace();
                }
     
            }
     
            // Log.d("GeoCoder", response);
            return formattedAddress;
        }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    程序是如何在 CPU 中运行的(二)
    程序是如何在 CPU 中运行的(一)
    结构体内存对齐解析
    如何以面向对象的思想设计有限状态机
    union 的概念及在嵌入式编程中的应用
    STM32 内存分配解析及变量的存储位置
    C语言如何实现继承及容器
    C语言指定初始化器解析及其应用
    pandas中的 where 和mask方法
    python中的类变量
  • 原文地址:https://www.cnblogs.com/shipeng22022/p/4613988.html
Copyright © 2011-2022 走看看