【问题标题】:How to get PlaceID from name, or lat, long in Android?如何在Android中从名称或纬度获取PlaceID?
【发布时间】:2016-03-20 21:42:56
【问题描述】:

这是我的代码:

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();
            }
        }

但我找不到 address.getPlaceID() 之类的东西。

也许我使用另一个 API 从上面的代码中获取名称或纬度的 PlaceID。谢谢!

【问题讨论】:

    标签: android google-geocoder


    【解决方案1】:

    看看这个docs。 您将获得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
    

    你会得到这样的回应

    {
      "html_attributions" : [],
      "results" : [
        {
          "geometry" : {
            "location" : {
              "lat" : -33.870775,
              "lng" : 151.199025
            }
          },
          ...
          "place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
          ...
        }
      ],
      "status" : "OK"
    }
    

    在那里,您将获得地点 ID。

    注意:请从Google Developer Console启用GeoLocation Api,否则会返回错误消息:This API project is not authorized to use this API.

    【讨论】:

    • 他们的服务条款限制了它在客户端应用程序中的使用(比如在安卓应用程序中)。你需要在服务器上运行它并缓存它。
    • 这是 web api,不是 android 的,OP 想用官方的 android sdk 来做这个
    • @JohnLeehey 那么我们如何从客户端设备上的 lat/lng 获取地点信息?
    • 抄送:@ÁbrahámEndre
    • @KhalilKhalaf 您需要在 google 注册一个结算帐户。之后,YOUR_API_KEY 将替换为该 google 帐户提供给您的地址,并根据您提出的请求数量向您收费。
    【解决方案2】:

    您也可以使用 direct 搜索代替 nearbysearch https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&amp;key=YOUR_API_KEY

    Getting Started with Geocoding

    【讨论】:

    • 这是地理编码 api 不是地点 api,因此它不会在响应中包含地点 ID
    • 这不起作用,它说我没有被授权? See Here
    【解决方案3】:

    这两种提议的方式都需要花钱,我认为人们应该提及它。我们从 Google 获得 -$200,但这完全取决于您拥有多少用户以及您需要使用地理编码的频率。每月向地理编码器发出 60k 次请求 - 只需 300 美元即可使用地理编码 https API:

    2018 年 11 月 1 日至 30 日 地理编码 API 地理编码:6073 次 - 30.38 美元

    不要像我们在其他帖子的请求中那样过滤您在回复中需要的内容?期待更多:

    2018 年 11 月 1 日至 30 日 Places API Places 详细信息:2389 计数 $40.61

    2018 年 11 月 1 日至 30 日 Places API 联系数据:2385 计数 $7.17

    2018 年 11 月 1 日至 30 日 Places API Atmosphere 数据:2385 个,计 11.95 美元

    【讨论】:

      【解决方案4】:

      我确实建议像这样使用获取地点 ID。这会比直接调用 API 更好。 我整天都在为这个问题绊倒。所以我希望它会有所帮助。谁正在寻找从您当前位置获取地点 ID 的方法。 安卓版

      科特林

      val mPlaceDetectionClient = Places.getPlaceDetectionClient(this.context!!)  
      val placeResult =  mPlaceDetectionClient.getCurrentPlace(null) 
      placeResult.addOnCompleteListener(object : OnCompleteListener<PlaceLikelihoodBufferResponse>{
          override fun onComplete(p0: Task<PlaceLikelihoodBufferResponse>) {
              val likelyPlaces = p0.result
              if(likelyPlaces.any()){
                  // get the place ID from code below
                  placeId = likelyPlaces[0].place.id  }
          }
      
      })
      

      或者如果谁正在实现或开发另一种语言,例如 java。您可以根据下面的参考来实现您的代码。

      java

      https://developers.google.com/places/android-sdk/googleapi-migration

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-07
        • 2014-04-14
        • 1970-01-01
        相关资源
        最近更新 更多