首页 文章

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

提问于
浏览
1

我正在尝试使用Google API时区,首先您必须对地址进行地理编码以获得时区的纬度/经度 . 我将如何从textarea中的值中执行此操作? 2个步骤,

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

https://maps.googleapis.com/maps/api/json?address=1600 Amphitheatre Parkway,Mountain View,CA&key = API_KEY

  • 然后使用此lat / long来获取时区示例

https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&key=API_KEY?

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

1 回答

  • 0

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

    还有另一个名为Google Maps Javascript API V3的API,这是您用来向客户端发出请求的API . 网站上有Geocode Examples

    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);
        }
      });
    }
    

    看到你要放置元素ID的第一行 getElementById() ,这是你原来的问题 .

    You do realize that this is all for getting the Timezone of an address that the user entered? right, you can probably make them choose a Timezone while they are entering an address.

    如果你只是想要用户的本地时区,那么这就容易多了,就这样:

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

相关问题