【问题标题】:Java Geocoding - convert latitude and longitude into an addressJava地理编码 - 将纬度和经度转换为地址
【发布时间】:2014-02-13 06:43:18
【问题描述】:

我的 Android 应用需要我的汽车当前所在城市的名称。

我有纬度和经度,想用地址中的地理编码器转换它,想得到城市。

我读了一些块,但因为我是新手,所以我不明白。

请任何人帮助我如何做到这一点?

编辑:

我没有在我的应用程序中使用此地理编码。我想在我的 Java Web 服务中使用它,我想我必须使用 HTTPRequest,是吗?并使用 google api url。

【问题讨论】:

标签: java android google-geocoder


【解决方案1】:

你要找的是Reverse Geocoding

Geocoder 类应该可以帮助您做您需要的事情(取自 here):

public static void getAddressFromLocation(
        final Location location, final Context context, final Handler handler) {
    Thread thread = new Thread() {
        @Override public void run() {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());   
            String result = null;
            try {
                List<Address> list = geocoder.getFromLocation(
                        location.getLatitude(), location.getLongitude(), 1);
                if (list != null && list.size() > 0) {
                    Address address = list.get(0);
                    // sending back first address line and locality
                    result = address.getAddressLine(0) + ", " + address.getLocality();
                }
            } catch (IOException e) {
                Log.e(TAG, "Impossible to connect to Geocoder", e);
            } finally {
                Message msg = Message.obtain();
                msg.setTarget(handler);
                if (result != null) {
                    msg.what = 1;
                    Bundle bundle = new Bundle();
                    bundle.putString("address", result);
                    msg.setData(bundle);
                } else 
                    msg.what = 0;
                msg.sendToTarget();
            }
        }
    };
    thread.start();
}

编辑:根据您的评论,不,您不能在普通 Java Web 服务上使用 Geocoder 类。话虽如此,还有其他选择。 Google Geocoding API 通常是一个不错的起点,话虽如此,他们似乎确实有 limits

或者,您可以查看Nominatim,它是一种开源反向地理编码服务,尽管与 Google 的服务相比它似乎略有限制。

【讨论】:

  • 抱歉忘了说。我不在我的应用程序中使用此地理编码。我有一个 java web 服务,但我不能使用“Geocoder”,是吗?我认为我必须通过 httprequest 和 google api 请求。
  • @aut_silvia:我已经更新了我的答案。我希望这能让你更清楚。
  • @JonathanRamos:说实话,这段代码不是我自己的,因此我包含了源代码。话虽如此,某些任务,例如资源获取(GPS 坐标、文件下载等)需要在 GUI 应用程序的单独线程上,因为将它们托管在主线程上通常会挂起 UI,这会降低用户应用程序的性能交互,在某些情况下,手机也可能会关闭应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 2014-01-16
  • 2021-04-15
  • 2010-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多