I am attempting to load some div's with google map images from a loop that adds one to the value of a document.get. Problem is the getLocation() call comes at the final iteration of the loop, but then iterates the loops n number of times.
for(var i = 1; i < length; i++){
var longitude = 1.8612393999999999;
var latitude = 50.7263312;
document.getElementById("longInput").value = longitude;
document.getElementById("latInput").value = latitude;
var mapholder = "mapholderM"+i;
document.getElementById("mapholderValue").value = mapholder;
theMapholder=document.getElementById(mapholder);
getLocation();
}
getLocation() uses longInput and latInput values for lat and lon variables, and theMapholder as a place to put the target div image. Maps are able to get the co ordinates and as explaned below on the final, or end of the loop also the theMapholder value.
I think this is not an issue with getLocation(), and more how I am attempting to use it. By adding an alert in the getLocation() call and viewing the elements on screen I know that the elements are populated though the loop and change, then get location is called n number of times after this happens. Then getLocation() only picks up the values that are set as the iterations of giving element values are complete.
Adding an alert at the beginning of the loop makes the loop go round n number of times then execute getLocation() only once.
Just to sum up: loops and changes element values, one by one. once this is complete getLocation() is called, with the same number of iterations. getLocation() can now only access the final setting of the elements so only gives one div a map.
Dean
Update: Sorry to have given incorrect information: it would appear any method call from my loop results in the same behaviour:
function pulse(){
$('#contentDiv').delay(200).fadeOut('slow').delay(50).fadeIn('slow');
}
Calling this method from my loop:
for(var i = 1; i < length; i++) {
var longitude = 1.8612393999999999;
var latitude = 50.7263312;
alert("end of loop");
pulse();
}
results in an alert n number of times, then pulse is executed? Sorry for confusion.
Dean
update: Google maps script:
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
alert($("#longInput").val()); */
lat= $("#latInput").val();
lon= $("#longInput").val();
latlon=new google.maps.LatLng(lat, lon);
mapholder=theMapholder;
alert("getLocation " + theMapholder);
var myOptions={
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
};
var map=new google.maps.Map(theMapholder,myOptions);
var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break;
}
}
</script>
Using the alert in showPosition I can see that it is only called after the loop has updated the elements.
Why would the a function behave like this, and only be called after the loop has executed everything else?