How to get a time zone from a location using latitude and longitude coordinates?

后端 未结 17 1553
走了就别回头了
走了就别回头了 2020-11-21 04:38

Given the latitude and longitude of a location, how does one know what time zone is in effect in that location?

In most cases, we are looking for an IANA/Olson time z

17条回答
  •  野的像风
    2020-11-21 05:16

    Here's how you can use Google's script editor to get the timezoneName and timeZoneId inside a gsheet.

    Step 1. Get an API key for Google's timezone API

    Step 2. Create a new gsheet. Underneath the 'tools' menu click 'script editor'. Add the following code:

    function getTimezone(lat, long) {  
      var apiKey = 'INSERTAPIKEYHERE'
      var url = 'https://maps.googleapis.com/maps/api/timezone/json?location=' + lat + ',' + long + '×tamp=1331161200&key=' + apiKey 
      var response = UrlFetchApp.fetch(url);
      var data = JSON.parse(response.getContentText());
      return data["timeZoneName"];
    }
    

    Step 3. Save and publish your getTimezone() function and use it as shown in the image above.

提交回复
热议问题