GoogleMaps CountryCode

by fguillen

HTML

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;signed_in=true"></script>
    <div id="panel">
        <input id="address" type="textbox" placeholder="CountryCode" />
        <input type="button" value="Geocode" id="geocode_button" />
    </div>
    <div id="map-canvas"></div>

CSS

#map-canvas {
        height: 400px;
        margin: 0px;
        padding: 0px;
        border: 1px solid red;
      }

JavaScript

var geocoder;
var map;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

function codeAddress() {
  var address = document.getElementById('address').value;
  geocoder.geocode({"address": address + " country"}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      map.fitBounds(results[0].geometry.bounds);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

$("#geocode_button").on("click", codeAddress);

initialize();