住所から緯度経度を取得する(ジオコーディング)

1600 Amphitheatre Parkway, Mountain View, CA とか 東京都千代田区千代田1-1 のように住所を入力して「緯度経度を取得」で、GeocoderResults オブジェクトを取得します。

このサンプルでは地図を描画しませんので、処理はいたってカンタンです。

<input type="text" name="address" id="address" value="東京都港区芝公園4-2-8">
<button type="button" id="submit-geocoding">緯度経度を取得</button>
<textarea id="result-geocoding">ここにレスポンス(JSON)が表示されます</textarea>
<script type="text/javascript">
  function initMap() {
    var geocoder = new google.maps.Geocoder;
    document.getElementById("submit-geocoding").addEventListener("click", function() {
      geocodeAddress(geocoder);
    });
  }
  function geocodeAddress(geocoder) {
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address}, function(results, status) {
      if (status === 'OK') {
        document.getElementById("result-geocoding").innerHTML = JSON.stringify(results, null , "  ");
        console.log(results);
      } else {
        window.alert('Geocoder failed due to: ' + status);
      }
    });
  }
</script>
<script async defer
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>