I have this function which is invoked upon clicking a button. The function creates a marker at a predefined position. I then use the elevation service for the given point. When I click the button, the alert message (I've put it there for diagnostics) displays 'undefined', and then the marker appears (in fact the marker should appear before the alert box!). I've tried my best to identify the source of the problem but in vain. Can anyone help me please? Thanks.
function buttonClicked () {
addNode(someLat, someLong);
alert(getElev(markers[0]));
}
function addNode(lat, lng) {
var LatLng = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({
position: LatLng,
});
marker.setMap(map);
markers.push(marker);
}
function getElev (marker) {
var ht = 0;
var locations = [];
var clickedLocation = marker.getPosition();
locations.push(clickedLocation);
var positionalRequest = {
'locations': locations
}
elevator.getElevationForLocations(positionalRequest, function (results, status) {
if (status == google.maps.ElevationStatus.OK && results[0])
ht = results[0].elevation;
});
return ht;
}