android - How to get place-id using geocode latlng address android
问题描述
I am using MapclickListener onclick of map getting latlng I am geocoding them I want the place id from that geocoded address and the places details using that address.
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));
}
});
解决方案
您必须进行两次 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 客户端初始化。
推荐阅读
- math - 当你不能有 0 和你不能有 1 时,它叫什么?
- nginx - Nginx - 反向代理 nginx: [emerg] mkdir() "/tmp/nginx/cache" failed (2: No such file or directory)
- javascript - 如何在for循环中只打印一个整数而不是整个数组在javascript中的结果?
- java - Spring ThreadPoolTaskExecutor中的maxPoolSize和throttleLimit有什么区别
- c++ - 三元运算符中是否强制复制省略(如果允许)?
- powershell - 如何让 powershell 一次运行所有这些 Invoke-Restmethod 命令?
- python - Spyder 索引网格在变量资源管理器中未对齐
- image-processing - 为什么 keras 测试生成器只返回批量大小作为数组形状的长度?
- python-3.x - 如何使用 pandas multiIndex 查询多列
- reactjs - Typescript 如何在 React 中键入对象的排序列