Getting back undefined from Geocode in Javascript v3 Google Maps API

I'm watching this code in the Chrome debugger. It keeps returning undefined for lat and lng values. I know that I am entering in real values that Google can geocode. I guess that somehow my $.get() must be executing before geocode fills the lat and lng values in. So how do I prevent this? Thanks.

function addItem() {
    var phone = $("#phone").val();
    var address = $("#address").val();
    var amount = $("#amount").val();
    var lat;
    var lng;
    geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            lat = results[0].geometry.location.lat;
            lng = results[0].geometry.location.lng;
            $.get(encodeURI("add.aspx?PhoneNumber=" + phone + "&Address=" + address + "&DollarAmt=" + amount + "&lat=" + lat + "&lng=" + lng + "&started=" + started)).done(function () { $("#orders").load("data.aspx") });
        }
        else {
            alert(status);
        }
    });
}

Open in new window

LVL 9
BobHavertyComhAsked:
Who is Participating?
 
Robert SchuttSoftware EngineerCommented:
lat and lng are functions, see the documentation here: https://developers.google.com/maps/documentation/javascript/reference#LatLng

so the solution is to replace lines 10 & 11 with this:
            lat = results[0].geometry.location.lat();
            lng = results[0].geometry.location.lng();

Open in new window

0
 
BobHavertyComhAuthor Commented:
Thank you Robert
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.