【问题标题】:How to get place-id using geocode latlng address android如何使用地理编码latlng地址android获取地点ID
【发布时间】:2018-07-10 06:25:28
【问题描述】:

我正在使用 MapclickListener onclick of map getting latlng 我正在对它们进行地理编码 我想要来自该地理编码地址的地点 ID 和使用该地址的地点详细信息。

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
   @Override
        public void onMapClick(LatLng latLng) {
            mMap.clear();
            mMap.addMarker(new MarkerOptions()
            .position(latLng));
            Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
            try {
              List<Address> addressList = geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);

             // Get geocode address 
             // ***https://maps.googleapis.com/maps/api/geocode/json?address=Address&key=GOOGLE_KEY***
             // Get place_id using this address
             // Get place details using the place_id

            } catch (IOException e) {
                e.printStackTrace();
            }
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM));
        }
    });

【问题讨论】:

标签: android google-maps google-places-api geocoding


【解决方案1】:

您必须进行两次 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

在这里你会得到 JSON

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

获得 json 后,下一步就是调用 -

https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJrTLr-GyuEmsRBfy61i59si0&key=YOUR_API_KEY

你会得到这样的 json

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "5",
            "short_name" : "5",
            "types" : [ "floor" ]
         }
      ],
      "adr_address" : "5, \u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e, \u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e \u003cspan class=\"region\"\u003eNSW\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
      "formatted_address" : "5, 48 Pirrama Rd, Pyrmont NSW 2009, Australia",
      "formatted_phone_number" : "(02) 9374 4000",
      "geometry" : {
         "location" : {
            "lat" : -33.866651,
            "lng" : 151.195827
         },
         "viewport" : {
            "northeast" : {
               "lat" : -33.8653881697085,
               "lng" : 151.1969739802915
            },
            "southwest" : {
               "lat" : -33.86808613029149,
               "lng" : 151.1942760197085
            }
         }
      },
      "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
      "id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
      "international_phone_number" : "+61 2 9374 4000",
      "name" : "Google",
      "place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
      "rating" : 4.5,
      "reference" : "CmRSAAAAjiEr2_A4yI-DyqGcfsceTv-IBJXHB5-W3ckmGk9QAYk4USgeV8ihBcGBEK5Z1w4ajRZNVAfSbROiKbbuniq0c9rIq_xqkrf_3HpZzX-pFJuJY3cBtG68LSAHzWXB8UzwEhAx04rgN0_WieYLfVp4K0duGhTU58LFaqwcaex73Kcyy0ghYOQTkg",
      "reviews" : [
         {
            "author_name" : "Robert Ardill",
            "author_url" : "https://www.google.com/maps/contrib/106422854611155436041/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/-T47KxWuAoJU/AAAAAAAAAAI/AAAAAAAAAZo/BDmyI12BZAs/s128-c0x00000000-cc-rp-mo-ba1/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "a month ago",
            "text" : "Awesome offices. Great facilities, location and views. Staff are great hosts",
            "time" : 1491144016
         }
      ],
      "scope" : "GOOGLE",
      "types" : [ "point_of_interest", "establishment" ],
      "url" : "https://maps.google.com/?cid=10281119596374313554",
      "utc_offset" : 600,
      "vicinity" : "5, 48 Pirrama Road, Pyrmont",
      "website" : "https://www.google.com.au/about/careers/locations/sydney/"
   },
   "status" : "OK"
}

但是:您也可以使用 google geocode lib,您应该在其中添加大量代码和 google 客户端初始化。

【讨论】:

  • 再多一张 onMapclick 我地理编码我会得到一个近似的不确切的做什么?
  • @SURYAN 我认为它会返回最近的地点 id
  • 是的,我会向您解释,例如 geocoder.getFromLocation(latLng.latitude,latLng.longitude) 它将提供最近的最大 20 个。我不需要 20 个最近的地方得到一个就足够了,geocoder.getFromLocation(latLng.latitude,latLng.longitude,1) 将提供最近的,即使点击的地方有一个 place_id
  • 如何克服这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
相关资源
最近更新 更多