java - 如何在Android中使用坐标(纬度,经度)获取地点ID

标签 java android google-maps

我用 谷歌地图 API 并且需要使用纬度和经度获取该地点的详细信息。

I've tried to get via Geocoder, but Class "Address" has no parameters PLACE_ID


Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
            List<Address> addresses = null;
            try {
                addresses = gcd.getFromLocation(52.2641, 76.9597, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (addresses.size() > 0) {
                Toast.makeText(this, addresses.get(0).getPlaceID(), Toast.LENGTH_SHORT).show();
            }
            else {
                // do your stuff
            }

最佳答案

要从坐标中获取地点 ID,您应该执行 reverse geocoding使用 REST API 进行查找。
原生 Android API 地理编码器不支持在 getFromLocation() 中获取地点 ID结果。不幸的是,Google Maps Android SDK 也不提供内置的地理编码器。功能请求存在很长时间,但看起来它没有高优先级:
https://issuetracker.google.com/issues/35823852
因此,要获得地点 ID,您必须使用 REST API。您可以在 github 上找到 Google Maps API Web Services 的 Java 客户端库:
https://github.com/googlemaps/google-maps-services-java
您可以使用此库从您的 Java 代码中调用 Geocoding API。

GeoApiContext context = new GeoApiContext.Builder()
    .apiKey("AIza...")
    .build();
GeocodingResult[] results =  GeocodingApi.newRequest(context)
    .latlng(new LatLng(52.2641, 76.9597)).await();
System.out.println(results[0].placeId);
请注意,网络服务的 API key 必须与您在 Google Maps Android SDK 中使用的 API key 不同,因为网络服务不支持 Android 应用限制。
还要考虑图书馆文档中的这一重要说明

The Java Client for Google Maps Services is designed for use in server applications. This library is not intended for use inside of an Android app, due to the potential for loss of API keys.

If you are building a mobile application, you will need to introduce a proxy server to act as intermediary between your mobile application and the Google Maps API Web Services. The Java Client for Google Maps Services would make an excellent choice as the basis for such a proxy server.


我希望这有帮助!

关于java - 如何在Android中使用坐标(纬度,经度)获取地点ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50618135/

相关文章:

java - 为什么我的 JTable 没有出现?

java - 当我在另一个应用程序中键入内容时,我的 Java 程序不会发出声音

java - 每当我创建一个 Activity 时,都会同时创建一个 content_activity 吗?

android - 谷歌地图拖动不顺畅

java - 从 Jython 中的 Java 项目访问方法

java - 如何解决MongoDB中的 "Digg"问题

iphone - Android - 数据存储?

android - INSTALL_FAILED_DUPLICATE_PERMISSION... C2D_MESSAGE

google-maps - 群集管理器,无法将自定义图标分配给MarkerOptions

jquery - jQuery 和 Google Maps API v3 的最佳插件?