调用Google Places API提取纯GPS坐标并去除多余字符的实现方案
- 点击此处查看我的Google Sheet可用示例
- 点击此处查看我的Google Apps Script代码
我有一个开发了一段时间的旅行类周边项目,它通过Google Places API拉取地点的相关数据并记录到Google Sheet中,之后我会将表格数据导入Google MyMap,在Google Maps上生成叠加层,方便我快速导航和查询信息。
我正在为表格需要填充的各个字段编写独立函数,包括营业时间、地址、Google Maps链接、商户官网等都会有单独的对应函数。
针对GPS坐标的获取,坐标会在地点的Geometry下的Location子段中返回。
点击此处查看BIG Alice Brewing的返回示例:
{ "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; }
完成代码
我按照如下方式整合了代码:
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(", "); }
内容的提问来源于stack exchange,提问作者Jeff Abrahams
相关产品推荐
相关产品推荐

