【问题标题】:Get zip code from longitude, latitude - android - Java从经度、纬度获取邮政编码 - android - Java
【发布时间】:2014-10-04 19:44:31
【问题描述】:

我已经在 SO 上尝试了几种可能的“解决方案”,但对我没有任何帮助。基本上,我正在尝试从 lat/lng 获取邮政编码。我读到Geocoder 和模拟器 Genymotion 存在一些问题,但我不确定这是否真的是问题所在。此外,我还尝试通过 HTTP 请求将邮政编码作为 JSON 对象获取,但这也不起作用。

这是我的代码:

@Override
    public void onLocationChanged(Location location) {

        EditText zipCode = (EditText) findViewById(R.id.zip_code);

        zipCode.setText(getZipCodeFromLocation(location));

    }

    private String getZipCodeFromLocation(Location location) {
        Address addr = getAddressFromLocation(location);
        return addr.getPostalCode() == null ? "" : addr.getPostalCode();
    }

    private Address getAddressFromLocation(Location location) {
        Geocoder geocoder = new Geocoder(this);
        Address address = new Address(Locale.getDefault());
        try {
            List<Address> addr = geocoder.getFromLocation(
                    location.getLatitude(), location.getLongitude(), 1);
            if (addr.size() > 0) {
                address = addr.get(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return address;
    }

问题是,addr.size() 是 0,我不知道为什么。

任何帮助将不胜感激。

【问题讨论】:

    标签: java android google-maps reverse-geocoding


    【解决方案1】:

    我对 Geocoder 对象一无所知,但我个人会使用 Google Geocoding Api。

    通过简单的 GET,您可以获得所需的信息。

    点击here查看官方文档。

    【讨论】:

      【解决方案2】:

      我以前也遇到过这样的问题,但不知道你的问题是否和你一样,因为你没有发布你的 logcat。如果您在 logcat 中收到“服务不可用”错误,则说明 Geocoder 存在错误。 https://stackoverflow.com/a/4045287/687245。如果是这样,有人提供了一种解决方法,可以通过对 Geocode 的 GET 调用来查询它。

      这是我从地理编码中获取纬度和经度的一种解决方法。其他人在 Stackoverflow 上发布了这段代码,所有的功劳都归于他。您所要做的就是将其更改为通过纬度和经度查询而不是字符串查询,并检索邮政编码而不是 lat lng。

      public void updateLocationByQuery(final String query) {
          if (mRunning)
              return;
      
          new AsyncTask<Void, Void, LatLng>() {
              protected void onPreExecute() {
                  mRunning = true;
              };
      
              @Override
              protected LatLng doInBackground(Void... params) {
                  LatLng p = null;
      
                  Geocoder geoCoder = new Geocoder(getActivity(),
                          Locale.getDefault());
                  try {
                      List<Address> addresses = geoCoder.getFromLocationName(
                              query, 1);
                      if (addresses.size() > 0) {
                          p = new LatLng(addresses.get(0).getLatitude(),
                                  addresses.get(0).getLongitude());
                      } else {
                          Toast.makeText(getActivity(),
                                  "Unable to find location by query: " + query,
                                  Toast.LENGTH_LONG).show();
                      }
                  } catch (Exception ignored) {
                      // after a while, Geocoder start to trhow
                      // "Service not availalbe" exception. really weird since it
                      // was working before (same device, same Android version
                      // etc..
                      ignored.printStackTrace();
                  }
      
                  if (p != null) // i.e., Geocoder succeed
                  {
                      return p;
                  } else // i.e., Geocoder failed
                  {
                      return fetchLocUsingGoogleMap(query);
                  }
              }
      
              // Geocoder failed :-(
              // Our B Plan : Google Map
              private LatLng fetchLocUsingGoogleMap(String query) {
                  double lng = 0, lat = 0;
                  query = query.replace("\n", " ").replace(" ", "%20");
                  String googleMapUrl = "http://maps.google.com/maps/api/geocode/json?address="
                          + query + "&ka&sensor=false";
                  try {
                      JSONObject googleMapResponse = new JSONObject(
                              ANDROID_HTTP_CLIENT.execute(new HttpGet(
                                      googleMapUrl), new BasicResponseHandler()));
      
                      lng = ((JSONArray) googleMapResponse.get("results"))
                              .getJSONObject(0).getJSONObject("geometry")
                              .getJSONObject("location").getDouble("lng");
      
                      lat = ((JSONArray) googleMapResponse.get("results"))
                              .getJSONObject(0).getJSONObject("geometry")
                              .getJSONObject("location").getDouble("lat");
      
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
                  return new LatLng(lat, lng);
              }
      
              @Override
              protected void onPostExecute(LatLng loc) {
                  mRunning = false;
                  if (loc != null) {
                      moveCamera(loc);
                  }
              };
          }.execute();
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多