【发布时间】:2021-07-26 15:56:20
【问题描述】:
See here for a working example of my Google Sheet
我有一个用于旅行的副项目which I have been working on for some time。它利用 Google Places API 来提取有关地点的相关数据并将其记录在 Google Sheet 中。从那里,我将工作表导入到 Google MyMap 中,这会在 Google 地图上创建一个叠加层,我可以使用它来快速导航和查找信息。
我正在尝试为要填写工作表的各个字段创建单独的函数。所以会有营业时间、地址、谷歌地图链接、营业网站等单独的功能。
当涉及到 GPS 坐标时,坐标会返回到地点的几何图形和位置子部分中。
BIG Alice Brewing 的Here is an example:
{
"html_attributions" : [],
"result" : {
"formatted_address" : "8-08 43rd Rd, Long Island City, NY 11101, USA",
"formatted_phone_number" : "(347) 688-2337",
"geometry" : {
"location" : {
"lat" : 40.75208620000001,
"lng" : -73.9505873
},
"viewport" : {
"northeast" : {
"lat" : 40.75347488029151,
"lng" : -73.94919081970849
},
"southwest" : {
"lat" : 40.75077691970851,
"lng" : -73.9518887802915
}
}
},
"name" : "Big aLICe Brewing",
"opening_hours" : {
"open_now" : true,
"periods" : [
{
"close" : {
"day" : 0,
"time" : "2000"
},
"open" : {
"day" : 0,
"time" : "1200"
}
},
{
"close" : {
"day" : 1,
"time" : "2100"
},
"open" : {
"day" : 1,
"time" : "1600"
}
},
{
"close" : {
"day" : 2,
"time" : "2100"
},
"open" : {
"day" : 2,
"time" : "1600"
}
},
{
"close" : {
"day" : 3,
"time" : "2100"
},
"open" : {
"day" : 3,
"time" : "1600"
}
},
{
"close" : {
"day" : 4,
"time" : "2100"
},
"open" : {
"day" : 4,
"time" : "1600"
}
},
{
"close" : {
"day" : 5,
"time" : "2200"
},
"open" : {
"day" : 5,
"time" : "1200"
}
},
{
"close" : {
"day" : 6,
"time" : "2200"
},
"open" : {
"day" : 6,
"time" : "1200"
}
}
],
"weekday_text" : [
"Monday: 4:00 – 9:00 PM",
"Tuesday: 4:00 – 9:00 PM",
"Wednesday: 4:00 – 9:00 PM",
"Thursday: 4:00 – 9:00 PM",
"Friday: 12:00 – 10:00 PM",
"Saturday: 12:00 – 10:00 PM",
"Sunday: 12:00 – 8:00 PM"
]
},
"types" : [ "food", "point_of_interest", "establishment" ],
"url" : "https://maps.google.com/?cid=10003627310223249349",
"website" : "http://bigalicebrewing.com/"
},
"status" : "OK"
}
如果我只想返回 40.75208620000001, -73.9505873 的 GPS 坐标,我需要做什么才能返回该值而无需额外的
`"lat" : 40.75208620000001,
"lng" : -73.9505873`
部分价值?
以下是我如何返回其余值的工作代码:
// This location basis is used to narrow the search -- e.g. if you were
// building a sheet of bars in NYC, you would want to set it to coordinates
// in NYC.
// You can get this from the url of a Google Maps search.
const LOC_BASIS_LAT_LON = "40.754734421655655, -73.98840133506883"; // e.g. "37.7644856,-122.4472203"
function COMBINED2(text) {
var API_KEY = 'AIzxxxxxxxxxxxxxxxxxxxlrE';
var baseUrl = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json';
var queryUrl = baseUrl + '?input=' + text + '&inputtype=textquery&key=' + API_KEY + "&locationbias=point:" + LOC_BASIS_LAT_LON;
var response = UrlFetchApp.fetch(queryUrl);
var json = response.getContentText();
var placeId = JSON.parse(json);
var ID = placeId.candidates[0].place_id;
var fields = 'name,formatted_address,formatted_phone_number,website,url,types,opening_hours';
var baseUrl2 = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=';
var queryUrl2 = baseUrl2 + ID + '&fields=' + fields + '&key='+ API_KEY + "&locationbias=point:" + LOC_BASIS_LAT_LON;
if (ID == '') {
return 'Give me a Google Places URL...';
}
var response2 = UrlFetchApp.fetch(queryUrl2);
var json2 = response2.getContentText();
var place = JSON.parse(json2).result;
var placeName = place.name;
var placeAddress = place.formatted_address;
var placePhoneNumber = place.formatted_phone_number;
var placeWebsite = place.website;
var placeURL = place.url;
var weekdays = '';
place.opening_hours.weekday_text.forEach((weekdayText) => {
weekdays += ( weekdayText + '\r\n' );
} );
var data = [ [
place.name,
place.formatted_address,
place.formatted_phone_number,
place.website,
place.url,
weekdays.trim()
] ];
return data;
}
完成的代码
我设法将 Irvin 的代码合并如下。
function GPS(text) {
//TEST PARSING
var API_KEY = 'AIzaSxxxxxxxxxxxxxxxxxxxx';
var baseUrl = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json';
var queryUrl = baseUrl + '?input=' + text + '&inputtype=textquery&key=' + API_KEY + "&locationbias=point:" + LOC_BASIS_LAT_LON;
var response = UrlFetchApp.fetch(queryUrl);
var json = response.getContentText();
var placeId = JSON.parse(json);
var ID = placeId.candidates[0].place_id;
var fields = 'name,geometry,formatted_address,formatted_phone_number,website,url,types,opening_hours';
var baseUrl2 = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=';
var queryUrl2 = baseUrl2 + ID + '&fields=' + fields + '&key='+ API_KEY + "&locationbias=point:" + LOC_BASIS_LAT_LON;
var responseAPI = UrlFetchApp.fetch(queryUrl2);
var content = responseAPI.getContentText();
var json = JSON.parse(content.toString()); //Sample JSON parsing
//An array variable to contain the longitude and latitude data
var inputArray = [];
//Parse the "location" JSON array contents
var gpsCoord = json.result.geometry.location
//Place the JSON contents into the array variable
for (var j in gpsCoord){
inputArray.push([gpsCoord[j]]);
}
return inputArray.join(", ");
}
【问题讨论】:
-
这是我的答案link的链接
标签: google-apps-script google-sheets google-places-api