2
function initialize(final) {
        /* ........ */
        var address_array = final.split('~');


        for (var count = 0; count < address_array.length; count++) 
        {
            if (geocoder) {
                geocoder.getLatLng(
                    address_array[count],
                    function (point) {
                        if (!point) {
                            alert(address_array[count] + " not found");
                        }

All I want is the alert to work over here, in the last line.

Suppose address_array has 3 values, i.e address_array.length is 3. But the alert is always showing undefined not found. I am assuming address_array[count] cannot be accessed from this function. However when I try

alert(address_array.length + " not found"); 

it says 3 not found. please help.

can anyone please help me regarding this issue?

function makeTheFunction(array, thisCount) { return function (point) { if (!point) { alert(array[thisCount] + " not found"); }

          else {
              var marker = new GMarker(point);
              map.addOverlay(marker);
              GEvent.addListener(marker, "click", function () {
                  marker.openInfoWindowHtml(array[thisCount] + "</b>");
              });
          }
      };
  }

the alert(array[thisCount] + " not found"); is working fine but it doenst seem to work when it goes into the else section ..marker.openInfoWindowHtml(array[thisCount] + "");

2 Answers 2

5

This happens because your function is a closure over the count variable, and closures get an enduring reference to the variable, not a copy of its value as of when the function is defined. So all copies of the function see count as of the end of the loop, which is outside the range of the array -- hence the undefined.

What you want to do is create the function such that it closes over something that doesn't change.

var address_array = final.split('~');

for (var count = 0; count < address_array.length; count++) 
{
    if (geocoder) {
        geocoder.getLatLng(
            address_array[count],
            makeTheFunction(address_array, count)
        );
    }
}

function makeTheFunction(array, thisCount)
{
    return function(point) {
        if (!point) {
            alert(array[thisCount] + " not found");
        }
    };
}

We pass both the array reference and the count value as of each iteration of the loop into makeTheFunction, which creates the function as a closure over its arguments and returns that function. Now the function will show the right information, because the information it's closing over doesn't change. Note that as written, makeTheFunction can be outside of any other function so as to avoid having the closure close over anything else.

More about closures: Closures are not complicated

Sign up to request clarification or add additional context in comments.

4 Comments

ya, even when i write alert(count + " not found"); it shows 3 not found. how to tackle with the problm??
@Anargha: I've included an example approach. My guess is I added it just after you saw the answer. :-)
ya. i saw ur answer but i m not able to grasp what exactly is going wrong.there is a 2nd part,the else part. there also i m not able to access the array[thiscount].. !!! else { var marker = new GMarker(point); map.addOverlay(marker); GEvent.addListener(marker, "click", function () { marker.openInfoWindowHtml(value + "</b>"); }); }
@Anargha: Regarding the second part, you just do the same thing I've done above: Create a function that creates the actual function you'll give to addEventListener. I think things will be clearer if you read through the article linked above.
1

Try:

function initialize(final) {
        /* ........ */
        var address_array = final.split('~');

        for (var count = 0; count < address_array.length; count++) 
        {
            doGeocode(address_array[count]);
        }
}

function doGeocode(value){
    if (geocoder) {
        geocoder.getLatLng(
        value,
        function (point) {
            if (!point) {
                alert(value + " not found");
            }
        }
     }
}

1 Comment

there is a 2nd part,the else part. there also i m not able to access the value. !!! else { var marker = new GMarker(point); map.addOverlay(marker); GEvent.addListener(marker, "click", function () { marker.openInfoWindowHtml(value + "</b>"); }); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.