From 701216297cb360f87c71cd6e08a467dbbff3089e Mon Sep 17 00:00:00 2001 From: Ivan Buttinoni Date: Wed, 6 Sep 2023 15:09:09 +0200 Subject: [PATCH 1/3] add fromPlaceId to get geocode infos by place_id --- src/index.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/index.js b/src/index.js index 46204e3..8182049 100644 --- a/src/index.js +++ b/src/index.js @@ -177,4 +177,39 @@ export default { return handleUrl(url); }, + + /** + * @see https://developers.google.com/maps/documentation/places/web-service/place-id + * + * @param {string} placeId + * @param {string} [apiKey] + * @param {string} [language] + * @param {string} [region] + * @returns {Promise} + */ + async fromPlaceId(placeId, apiKey, language, region) { + if (!placeId) { + log("Provided place_id is invalid", true); + return Promise.reject(new Error("Provided place_id is invalid")); + } + + let url = `${GOOGLE_API}?place_id=${encodeURIComponent(placeId)}`; + + if (apiKey || API_KEY) { + API_KEY = apiKey || API_KEY; + url += `&key=${API_KEY}`; + } + + if (language || LANGUAGE) { + LANGUAGE = language || LANGUAGE; + url += `&language=${LANGUAGE}`; + } + + if (region || REGION) { + REGION = region || REGION; + url += `®ion=${encodeURIComponent(REGION)}`; + } + + return handleUrl(url); + }, }; From 4ff5006a2c9e49fd6a812fe275c1419a59ec5aa0 Mon Sep 17 00:00:00 2001 From: Ivan Buttinoni Date: Wed, 6 Sep 2023 15:09:51 +0200 Subject: [PATCH 2/3] add docs and sample for fromPlaceId --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5b61ba8..1c9b461 100644 --- a/README.md +++ b/README.md @@ -104,12 +104,26 @@ Geocode.fromAddress("Eiffel Tower").then( console.error(error); } ); + +// Get address, latitude & longitude from place_id. +// useful to get the same address informations in different languages +// ChIJgzdRtHRhLxMRY-YxN1kxig4 Piazza del Colosseo, 1, 00184 Roma RM, Italy +// EipQLnphIFNhbiBNYXJjbywgMiwgMzAxMjQgVmVuZXppYSBWRSwgSXRhbHkiMBIuChQKEgmbk_5117F-RxH78rNJtR36wBACKhQKEglFSalx17F-RxHsO4mJJuHg5A P.za San Marco, 2, 30124 Venezia VE, Italy +Geocode.fromAddress("ChIJPfSfhdexfkcRyMPGCbQyI04").then( + (response) => { + const address = response.results[0].formatted_address; + console.log(formatted_address); + }, + (error) => { + console.error(error); + } +); ``` #### Methods | Method | Arguments | Params | Type | Description | -| :-------------- | :--------------------------------------------------------- | :--------: | :--------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| :-------------- |:-----------------------------------------------------------| :--------: | :--------: |:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | setApiKey | `api_key` | - | `function` | set [Google Maps Geocoding API](https://developers.google.com/maps/documentation/geocoding/intro) for purposes of quota management. Its optional but recommended | | setLanguage | `language` | - | `function` | Specify language of the parsed address. [List of the available language codes](https://developers.google.com/maps/faq#languagesupport). Defaults to english | | setRegion | `region` | - | `function` | Specify region of the parsed address. | @@ -117,6 +131,7 @@ Geocode.fromAddress("Eiffel Tower").then( | enableDebug | `true` or `false` | - | `function` | Enable or disable logs. Its optional. | | fromLatLng | `latitude`, `longitude`, `*apiKey`, `*language`, `*region` | `response` | `function` | Get address from latitude & longitude. \* Optional arguments | | fromAddress | `address`, `*apiKey`, `*language`, `*region` | `response` | `function` | Get latitude & longitude from address. \* Optional arguments | +| fromPlaceId | `place_id`, `*apiKey`, `*language`, `*region` | `response` | `function` | Get address from place_id. \* Optional arguments. [Overview on Place IDs](https://developers.google.com/maps/documentation/places/web-service/place-id) | ### Follow me on Twitter: [@shukerullah](https://twitter.com/shukerullah) From 1bbf0f5257ed8a47675f6516ddaeb3e7c28c3ba4 Mon Sep 17 00:00:00 2001 From: Ivan Buttinoni Date: Wed, 6 Sep 2023 15:11:21 +0200 Subject: [PATCH 3/3] fix documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c9b461..8935d71 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ Geocode.fromAddress("Eiffel Tower").then( // useful to get the same address informations in different languages // ChIJgzdRtHRhLxMRY-YxN1kxig4 Piazza del Colosseo, 1, 00184 Roma RM, Italy // EipQLnphIFNhbiBNYXJjbywgMiwgMzAxMjQgVmVuZXppYSBWRSwgSXRhbHkiMBIuChQKEgmbk_5117F-RxH78rNJtR36wBACKhQKEglFSalx17F-RxHsO4mJJuHg5A P.za San Marco, 2, 30124 Venezia VE, Italy -Geocode.fromAddress("ChIJPfSfhdexfkcRyMPGCbQyI04").then( +Geocode.fromPlaceId("ChIJPfSfhdexfkcRyMPGCbQyI04").then( (response) => { const address = response.results[0].formatted_address; console.log(formatted_address);