如何使用Google API从地址值中获取时区

How to get timezone from address value with Google API?

本文关键字:获取 时区 地址 何使用 Google API      更新时间:2023-09-26

我正在尝试使用Google API时区,首先您必须对地址进行地理编码才能获得时区的lat/long。如何从文本区域中的值开始执行此操作?2步,

  1. 将textarea值转换为地理编码示例

https://maps.googleapis.com/maps/api/json?address=1600+露天剧场+公园大道,+山+景,+CA&密钥=API_key

  1. 然后使用这个lat/long来获得时区示例

https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&时间戳=1331161200&key=API_key?

<textarea id="phyaddr">
123 Address
Suite 1200
Houston TX 77008     
USA
County:Harris
</textarea>

Google WebService API适用于服务器端的GeoCoding,您拥有的url将从服务器发送,这就是为什么它包含密钥,您不会将该密钥放在面向客户端的代码中的任何位置。

还有另一个API,名为Google Maps Javascript API V3,这是您用来在客户端进行请求的API。网站上有这样做的地理代码示例:

function codeAddress() {
  var address = document.getElementById('address').value;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

请参阅第一行getElementById(),它是您放置元素ID的地方,这是您最初的问题。

你知道这都是为了获取用户输入的地址的时区吗?对,你可以让他们在输入地址时选择时区

如果你只是想要用户的本地时区,那么这要容易得多,只需要这个:

// this is offset from UTC time
var offset = new Date().getTimezoneOffset();