Skip to content Skip to sidebar Skip to footer

Uncaught Typeerror: Cannot Read Property 'lat' Of Undefined

I am trying to make a website using the google maps javascript API to find hotels around a city, and compare the distances between each hotel to user-entered locations in order to

Solution 1:

The geocoder is asynchronous, you can't return anything from its callback function. You have to use the data inside the callback function when/where it is available. Duplicate of How do I return the response from an asynchronous call?

This is incorrect:

functiongeocodeAddress(geocoder, address) {
  console.log("address " + address);
  console.log("geocoder " + geocoder);
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      var coords = results[0].geometry.location.toString();
      console.log("CoordsRaw: " + coords)
      var coordsNoFirst = coords.substring(1);
      var coordsNoLast = coordsNoFirst.slice(0, -1);
      cityCoordinates = coordsNoLast;
      console.log(cityCoordinates);

      //*********** this will not do anything useful **********return results[0].geometry.location;
      //*******************************************************

    } else {
      alert('Could not convert an address to latitude/longitude for the google maps API: ' + status);
    }
  });
}

Post a Comment for "Uncaught Typeerror: Cannot Read Property 'lat' Of Undefined"