0

I am trying to get elevation data on click in google maps api. for that following is what I have implemented.

function getElevationtimeer(lat, lng) {
            returnvaluetime = "";
            elevationpoint = "";
            getElevation(lat, lng);
            setTimeout(ajax, 3000);
            return returnvaluetime;
}

function getElevation(lat, lng) {
            var locations = [];
            elevator = new google.maps.ElevationService();
            var clickedLocation = new google.maps.LatLng(lat, lng);
            locations.push(clickedLocation);
            var positionalRequest = {
                'locations': locations
            }
            elevator.getElevationForLocations(positionalRequest, function (results, status) {
                if (status == google.maps.ElevationStatus.OK) {
                    if (results[0]) {
                        elevationpoint = results[0].elevation;
                    } else {
                    }
                } else {
                }
            })
            return returnvaluetime;
        }

function ajax() {
            if (elevationpoint != "")
                returnvaluetime = elevationpoint;
        }

Initially I am calling getElevationtimeer method. From that I am calling first getElevation method, in which I am finding elevation for clicked location. After that I am calling ajax method, in which I am assigning elevation value to the variable which I want to return from main method.

I can understand what is going wrong in above code. Please help me what is possible way to implement the logic for getting elevation.

  • You may use promise to solve your issue http://kctang.github.io/blog/2015/05/09/understanding-javascript-promise-with-examples-no-more-callback-hell/ – Zesky May 15 '15 at 08:10
  • None of your functions return anything but empty strings or undefined variables. If you include some of the following I may be able to help: * Your "main method" implementation * Your AJAX implimentation – thodic May 15 '15 at 08:11
  • I am getting value from elevator.getElevationForLocations(positionalRequest, function (results, status) { if (status == google.maps.ElevationStatus.OK) { if (results[0]) { elevationpoint = results[0].elevation; }) I am calling getElevationtimeer function from server side. So yes I am getting empty string on the server side. because elevator.getElevationForLocations takes time to be executed. ----OhAuth – WebAshlar EmailTest May 15 '15 at 08:41
  • @WebAshlarEmailTest `returnvaluetime` is not being set inside `getElevation()` so it will always return `undefined`. – thodic May 15 '15 at 08:45
  • Oh, so should I set it in side getElevatio()??? Right I have set it globally. – WebAshlar EmailTest May 15 '15 at 08:48

0 Answers0