0

I'm using google maps API to get elevation value by LAT and LON but I'm getting always undefined objects.

My script is this:

<script type="application/javascript">
        function findAltitude() {
            var lat  = document.getElementById('lat').value;
            var lon  = document.getElementById('lon').value;

            var url = "https://maps.googleapis.com/maps/api/elevation/json?locations=11,11&key=<API_KEY>";

            var json, data;

            $.ajax({
                  dataType: "json",
                  url: url,
                  data: data,
                  success: function(data) {
                       alert(data.results.elevation);
                    }
                });
        }

  </script>

and my JSON is this:

{"results" : [
  {
     "elevation" : 402.0888977050781,
     "location" : {
        "lat" : 11,
        "lng" : 11
     },
     "resolution" : 152.7032318115234
  }],"status" : "OK"}
0

3 Answers 3

3

results in the posted JSON contains an array. so you might want to log e.g. the first results elevation like so: alert(data.results[0].elevation);

Sign up to request clarification or add additional context in comments.

Comments

1

Results is an array hence your issue.

Change this line:

data.results.elevation

With:

 data.results[0].elevation

Comments

1

You can do

        $.ajax({
              dataType: "json",
              url: url,
              data: data,
              success: function(data) {
                   alert(data.results[0].elevation);
                }
            });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.