如何在Android中从名称或lat获取PlaceID?

rom*_*e 웃 8 android google-geocoder

这是我的代码:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocationName(text, 10);
    test.clear();
    for (int i = 0; i < addresses.size(); i++) {
        Address address = addresses.get(i);
        for (int j=0;j<address.getMaxAddressLineIndex();j++) {
            test.add(address.getAddressLine(j) + ", " + address.getCountryName());
//                address.getLatitude();
//                address.getLatitude();
            }
        }
Run Code Online (Sandbox Code Playgroud)

但我找不到像address.getPlaceID()这样的东西.

也许我使用另一个API来自name或lat,从上面的代码获取PlaceID.谢谢!

Soh*_*ham 9

看看这个文档.您将获得lat,long,名称Address ,然后在google api下面调用

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=YOUR_API_KEY
Run Code Online (Sandbox Code Playgroud)

你会得到这样的回复

{
  "html_attributions" : [],
  "results" : [
    {
      "geometry" : {
        "location" : {
          "lat" : -33.870775,
          "lng" : 151.199025
        }
      },
      ...
      "place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
      ...
    }
  ],
  "status" : "OK"
}
Run Code Online (Sandbox Code Playgroud)

在那里你将获得place-id.

注意:请从Google Developer Console启用GeoLocation Api,否则会返回错误消息:此API项目无权使用此API.

  • 他们的服务条款限制了它在客户端应用程序中的使用(如在Android应用程序中).您需要在服务器上运行它并缓存它. (2认同)