I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:
heights=[];
function getElevationInt er(latv,lngv) {
var script = document.create Element('script ');
document.body.a ppendChild(scri pt);
script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
'&lng=' + lngv + '&callback=load JSON';
}
function loadJSON(result ) {
heights.push(re sult.srtm3);
}
function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
getElevationInt er(latPoints,ln gPoints);
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}
}
Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
I would be grateful for any assistance.
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:
heights=[];
function getElevationInt er(latv,lngv) {
var script = document.create Element('script ');
document.body.a ppendChild(scri pt);
script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
'&lng=' + lngv + '&callback=load JSON';
}
function loadJSON(result ) {
heights.push(re sult.srtm3);
}
function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
getElevationInt er(latPoints,ln gPoints);
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}
}
Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
I would be grateful for any assistance.
Comment