'How to get timezone from address value with Google API?
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,
- convert textarea value to geocode example
- then use this lat/long to get the timezone example
<textarea id="phyaddr">
123 Address
Suite 1200
Houston TX 77008
USA
County:Harris
</textarea>
Solution 1:[1]
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();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
