Google maps api geocoder is not working

Viewed 7684

I am trying to convert a zip code into lat and lng using geocoder. I looked at the Google Maps Api example and followed several tutorials, but for some reason for lat and lng it returns "". Here is my code.

(function(window, google, address) {
  var geocoder = new google.maps.Geocoder();
  var lat = "";
  var lng = "";
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    var test = results[0];
    lat = results[0].geometry.location.lat(); // lat is still equal to ""
    lng = results[0].geometry.location.lng(); // lng is still equal to ""
  });

  var options = {
      center: {
        lat: lat,
        lng: lng
      },
      zoom: 5
    },
    element = document.getElementById('map-canvas')
  var map = new google.maps.Map(element, options)
}(window, window.google, 98107));
html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>

2 Answers
Related