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

问题描述 投票:1回答:1

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

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

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

  1. 然后使用此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>
javascript jquery timezone google-api geocoding
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(),这是你原来的问题。

您是否意识到这只是为了获取用户输入的地址的时区?是的,你可以让他们在输入地址时选择一个时区。

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

// this is offset from UTC time
var offset = new Date().getTimezoneOffset();
© www.soinside.com 2019 - 2024. All rights reserved.