Javascript – How to get timezone from address value with Google API

geocodinggoogle apijavascriptjquerytimezone

I am trying to use the Google API time zones, and first you must geocode the address to get the lat/long for time zone.
How would I go about doing this from a value in a textarea? 2 steps,

  1. convert textarea value to geocode example

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

  1. then use this lat/long to get the timezone example

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>

Best Answer

The Google WebService API is meant for server side GeoCoding , that url you have is to be sent from the sever, that is why it contains a key, you would not put that key anywhere in the client facing code.

there is another API called Google Maps Javascript API V3 , this is the one you use to make requests client side . There are Geocode Examples on the site of to do that :

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

see the first line getElementById() that is where you would put the ID of your element , which was your original question .

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.

if you simply just want the user's local timezone , well then that is much much easier , just this:

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