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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobHavertyComhAuthor Commented:
Thank you Robert
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.