0

My code:

    function geocodeAddress(address){
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode(
            {
                'address':address   
            },
            function(results,status){
                if(status===google.maps.GeocoderStatus.OK){ 
                    var resultsObj = results[0].geometry;
                    var resultsLocationObj = resultsObj['location'];
                    var lat = resultsLocationObj['k'];
                    var lon = resultsLocationObj['D'];
                    var coordinatesObj = {
                        address : address,
                        latitude : lat,
                        longitude : lon
                    };
                    return coordinatesObj;
                }
            }
        );
    }

$(document).ready(function(){
function initialize(){
        $('#save-address').click(
            function saveAddress(){
                var addressText = $('#address').val();
                console.log(geocodeAddress(addressText));
            }
        );
}
    google.maps.event.addDomListener(window, 'load', initialize);

})

When the function runs, two things are printed to the console:

  1. undefined at line XXXX, where XXXX is the line of the second console.log().
  2. The value for coordinatesObj (an object containing the address, latitude and longitude)

I want the second console.log() to show the value of coordinatesObj.

What do I do?

RBPB
  • 73
  • 1
  • 1
  • 4

1 Answers1

1

Without seeing your markup, we can't say for sure what's happening, but from the code you've shown- I suspect that there is no HTMLInputElement element with "address".

$("#invalidId").val(); // returns `undefined`
bvaughn
  • 13,300
  • 45
  • 46