如何在安卓中使用坐标(纬度、纬度)获取地点 ID



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

我试图通过地理编码器获取,但类">地址"没有参数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,您应该使用 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 代码调用地理编码 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 密钥必须与您在 Google 地图 Android SDK 中使用的 API 密钥不同,因为网络服务不支持 Android 应用限制。

还要考虑库文档中的这一重要说明

Google Maps Services 的 Java Client 设计用于服务器应用程序。此库不适合在 Android 应用内部使用,因为可能会丢失 API 密钥。

如果您正在构建移动应用程序,则需要引入代理服务器作为移动应用程序和 Google Maps API Web 服务之间的中介。谷歌地图服务的Java客户端将是一个很好的选择,作为这种代理服务器的基础。

我希望这有帮助!

您可以在标记的底部获得位置 Id。 像这样:

mGoogleMap.setOnPoiClickListener(new GoogleMap.OnPoiClickListener() {
@Override
public void onPoiClick(PointOfInterest pointOfInterest)
{
addOrSaveMarkerToMap(pointOfInterest.latLng.latitude, pointOfInterest.latLng.longitude, pointOfInterest.name, "", pointOfInterest.placeId, true, true);

//Toast.makeText(GoogleMapsActivity.this,""+pointOfInterest.name+" (lat: "+pointOfInterest.latLng.latitude+", long: "+pointOfInterest.latLng.longitude+") " +" is added in your favorite list",Toast.LENGTH_LONG).show();
}
});

如果您想阅读更多关于什么是兴趣点,请查看此文档链接。

https://developers.google.com/maps/documentation/android-sdk/poi

最好的方法是使用google places API.它将为您提供有关特定地点的完整信息。

如果您想使用google places API请点击此链接:

如何使用谷歌地方 API 在安卓中通过 latLng 获取地点或位置 ID?

仅供参考

Geocoder没有提供getPlaceID().

提供地点 ID 以查找给定地点 ID 的地址。这个地方 ID 是可与其他 Google API 一起使用的唯一标识符

地理编码器请求对象文本包含以下字段:

{
address: string,
location: LatLng,
placeId: string,
bounds: LatLngBounds,
componentRestrictions: GeocoderComponentRestrictions,
region: string
}

您应该阅读Retrieving an Address for a Place ID

/**
* Requests the details of a Place.
*
* <p>We are only enabling looking up Places by placeId as the older Place identifier, reference,
* is deprecated. Please see the <a
* href="https://web.archive.org/web/20170521070241/https://developers.google.com/places/web-service/details#deprecation">
* deprecation warning</a>.
*
* @param context The context on which to make Geo API requests.
* @param placeId The PlaceID to request details on.
* @return Returns a PlaceDetailsRequest that you can configure and execute.
*/
public static PlaceDetailsRequest placeDetails(GeoApiContext context, String placeId) {
PlaceDetailsRequest request = new PlaceDetailsRequest(context);
request.placeId(placeId);
return request;
}

试试这个

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1); 
// Here 1 represent max location result to returned, by documents it recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); 
// If any additional address line present than only, check with max 
available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); 
// Only if available else return NULL

最新更新