I have an array with localizations ("street, city, postalcode, province, country").
I want to have a string with all Lat and Lng values for each localization, like this ("#lat.value|lng.value#lat2.value|lng2.value.." etc). I use for this Google API Geocoder. But Geocoder has a annoying limit 10~11 localizations once. How can I get about 100 lat&lng values for 100 localization? I tried sleep() funcion, but this doesn’t work. This is my function in js.
function sleep(time){
time = time * 1000;
var start = (new Date()).getTime();
while(true){
alarm = (new Date()).getTime();
if(alarm - start > time){ break; }
}
}
(..)
var mystring = "";
var address = <%=testing %>
var arrayaddress = address.split("%");
for (var i = 0; i < arrayaddress.length-1; i++) {
sleep(0);
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': arrayaddress[i]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var zmiennalat = marker.getPosition().lat();
var zmiennalng = marker.getPosition().lng();
mystring = mystring + "#" + zmiennalat + "|" + zmiennalng;
}
else
{
mystring = mystring + "#" + "er" + "|" + "er";
//alert("Geocode was not successful for the following reason: " + status);
}
if (i == arrayaddress.length-1) {
var dsa = mystring;
document.getElementById("<%=hfWysokosc.ClientID%>").value = mystring;
document.getElementById("<%=hfSzerokosc.ClientID%>").value = dsa;
document.getElementById("<%=UpdateButton1.ClientID %>").click();
}
});
} // for
Then, result is for example:
er|er#34.42342|16.4323#23.32131|43.54545# etc..
always at first localizations is error with decoding (if more than 10 places in array). Why?
Why not at last localizations?
Resuming: How can i get more than 10 lat|lng values in this stirng with this loop?
Thanks!
EDIT
thanks, that work nice !
But there is another problem. string Mystring is showing very slowly.. how to repair it? and it is refreshing when marker is setting on map – maybe it is not a problem, but for 100-150 markers i have to wait a few long minutes to get Mystring value.. Is it any posibility to show more faster Mystring value?
var mystring = "";
var address = <%=testing %>
alert(address);
var arrayaddress = address.split("%");
(function nextStep(i){
if( i >= arrayaddress.length-1 ){
return;
}
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': arrayaddress[i]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var zmiennalat = marker.getPosition().lat();
var zmiennalng = marker.getPosition().lng();
mystring = mystring + zmiennalat + "|" + zmiennalng + "#";
}
else
{
mystring = mystring + "er" + "|" + "er" + "#";
//alert("Geocode was not successful for the following reason: " + status);
}
});
setTimeout(function(){ nextStep(i+1); }, 800);
var dsa = mystring;
document.getElementById("<%=hfSzerokosc.ClientID%>").value = dsa;
document.getElementById("<%=UpdateButton1.ClientID %>").click();
})(0);
UPDATE
function nextStep(i){
if( i >= arrayaddress.length-1 ){
return;
}
// code
});
setTimeout(function(){ nextStep(i+1); }, 1000);
};
then
<button type="button" onclick="nextStep(0)">Click Me!</button>
and error is:
nextStep is not defined
what is wrong?
Instead of for-loop, try this:
UPDATE: If you want to run
nextStepfunction,only when button will be clicked,then modify above code like this:Then,when
onclickwill be triggered, simply callnextStep(0).