使用 Google Places API 删除 GPS 请求周围的多余字符

Remove extra characters around GPS request with Google Places API

See here for a working example of my Google Sheet

Here is my Google Apps Script


我有一个用于旅行的副业 。它利用 Google 地点 API 来提取有关地点的相关数据并将其记录在 Google Sheet 中。从那里,我将 Sheet 导入到 Google MyMap 中,它在 Google 地图上创建了一个叠加层,我可以使用它来快速导航和查找信息。

我正在尝试为我想要填写 Sheet 的各个字段创建单独的函数。所以营业时间、地址、Google地图link、商家网站等都会有单独的功能

当涉及到 GPS 坐标时,坐标 return 在地点的几何图形下和位置子部分内。

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"
}

如果我只想 return 40.75208620000001, -73.9505873 的 GPS 坐标,我需要做什么才能 return 在没有额外

`"lat" : 40.75208620000001,
            "lng" : -73.9505873`

部分值?

下面是我如何 return 我的其余价值观的工作代码:

// 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(", "); 


}

建议

您可以尝试将“result.geometry.location”的 JSON 数组值放入 Apps 脚本代码的数组变量中。您可以参考下面的示例脚本,然后相应地调整您的脚本:

示例脚本:

 function gpsCoordinate() {
    //TEST PARSING
    var responseAPI = UrlFetchApp.fetch("https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJh9-J99hYwokRxc9AgwkG1Io&fields=name,formatted_address,geometry,formatted_phone_number,website,url,types,opening_hours&key=AIzaSyD1FOrIRLbO9mV4zbh6df0os6ZCeaGKlrE&locationbias=point:42.6408341747088,18.109130742689384"); 
    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; 
  }

Note: Used the code from Google Apps Script to Output a JSON Array into a row as reference

示例结果:

The gpsCoordinate() method only returned the longitude & latitude values from the JSON result: