最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get timezone from address value with Google API? - Stack Overflow

programmeradmin1浏览0评论

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

+Amphitheatre+Parkway,+Mountain+View,+CA&key=API_KEY

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

.6034810,-119.6822510&timestamp=1331161200&key=API_KEY?

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

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./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./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>
Share Improve this question edited Feb 27, 2018 at 13:08 Vivek 3321 silver badge13 bronze badges asked Sep 22, 2014 at 17:04 triplethreat77triplethreat77 1,2968 gold badges35 silver badges70 bronze badges 4
  • 2 is that a question? It looks like you just described how to do it... if your looking for someone here to code it for you , they won't . write the code yourself , then paste it here with specific parts you're having problems with – Scott Selby Commented Sep 22, 2014 at 17:06
  • I need to know how to get the textarea value to url form with plus and mas, then grab the lat/long from the geocode output. – triplethreat77 Commented Sep 22, 2014 at 17:10
  • added a new answer , I think you are confused over the different API's that Google offers – Scott Selby Commented Sep 22, 2014 at 17:45
  • You are correct in the calls you have to make. But you should read the documentation for The Google Geocoding API and The Google Time Zone API. Also, search around a bit. There are plenty of questions about each of these. If you get stuck, show the code you tried when asking. So far, all you've done is made a request for someone to write code for you - which is not what StackOverflow is for. – Matt Johnson-Pint Commented Sep 22, 2014 at 22:59
Add a ment  | 

1 Answer 1

Reset to default 0

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();
发布评论

评论列表(0)

  1. 暂无评论