0

I am working on google Maps API. I dont know why the below function is being called after the index++. As far as I know ReverseGeocode() should be called first. Instead of that it is first incrementing and then calling the function which is creating problems for me. The alert boxes are shown as they are written but the middle function is called after the last line of the function is executed i.e (index++).

      function placeMarker(location)
      {
          alert("iiii");
          ReverseGeocode(location.lat(),location.lng());
          alert("jjjk");
          index++;
      }

Here is my ReverseGeoCode

  function ReverseGeocode(lat,lng) {     
     var latlng = new google.maps.LatLng(lat, lng);
     geocoder.geocode({'latLng': latlng}, function(results, status)
  {
      if (status == google.maps.GeocoderStatus.OK) 
     {
           if (results[1])
      {

          places[index]=results[0].formatted_address;
          alert(places[index]+"index="+index);
          AddRow('table',results[0].formatted_address);
          document.getElementById("dataa").innerHTML+=results[0].formatted_address+"<br/>";
      }
  }
  else 
  {
    alert("Geocoder failed due to: " + status);
      }
    });
  }

Please Explain. Thanks in advance.

6
  • 1
    Can you post your ReverseGeocode function? Commented Apr 22, 2012 at 10:26
  • How do you determine that ReverseGeoCode executes after index++? Commented Apr 22, 2012 at 10:31
  • @deestan look at the reversegeocode function i've placed an alertbox in it.the alertbox shows the index value as 1 and i've defined it globally as 0.So when the first time function is called the alert box shows me the index value of 1 Commented Apr 22, 2012 at 10:33
  • Is geocoder.geocode asynchronous? Commented Apr 22, 2012 at 10:38
  • you could add a callback as a third arg to your GeoCode function and execute it at the end of the function; then you can pass any dependent operations such as your index increment into the callback. Commented Apr 22, 2012 at 10:40

2 Answers 2

0

The alert is inside your callback function, which will execute when geocoder.geocode finishes its calculation.

geocoder.geocode appears to asynchronous. Usually, this means geocoder.geocode will start plodding along with its work somewhere else, while your program continues to its local conclusion. When geocoder.geocode later finishes, it will execute your supplied callback function.

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

Comments

0

I think that the geocoder.geocode is asynchronous. It is executing your anonymous function somethime later, when the value of index has incremented.

function placeMarker(location)
 {
 alert("iiii")
 ReverseGeocode(location.lat(),location.lng(),index);
 alert("jjjk");
 index++;

    }

 

function ReverseGeocode(lat,lng,index) {     
     var latlng = new google.maps.LatLng(lat, lng);
     geocoder.geocode({'latLng': latlng}, function(results, status)
  {
      if (status == google.maps.GeocoderStatus.OK) 
     {
           if (results[1])
      {

          places[index]=results[0].formatted_address;
          alert(places[index]+"index="+index);
          AddRow('table',results[0].formatted_address);
          document.getElementById("dataa").innerHTML+=results[0].formatted_address+"<br/>";
      }
  }
  else 
  {
    alert("Geocoder failed due to: " + status);
      }
    });
  }

In this case, index goes into the local scope of the anonymous function, so it is not overwritten.

Comments

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.