Recursive Find Calls

When using the AJAX version of the Virtual Earth control people have had issues making multiple find calls, one after the other. The resulting information does not always return in the order expected. The way around this issue is to use recursion. This will essentially make one call at a time and wait for one find call to finish before making another. The following code demonstrates how to do this:

 

<html>
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script>
      <script>
         var map = null;
         var count = 0;
         var locations = ["Toronto, Ontario","New York, New York","Seattle, WA"];
         function GetMap()
         {
            map = new VEMap(‘myMap’);
            map.LoadMap();
         }  

         function FindLocation(searchstr)
         {
            if (searchstr != ”)
            {
                map.Find(null, searchstr, null, null, null, null, false, null, null, false, AddPin);
            }
         }
         function AddPin(layer, resultsArray, places, hasMore, veErrorMessage)
         { 
            if(places.length > 0)
            {
                var shape = new VEShape( VEShapeType.Pushpin,places[0].LatLong);
                shape.SetTitle(places[0].Name);
                map.AddShape(shape);
            }
            count++;
            if(count<locations.length)
                FindLocation(locations[count]);
         } 
      </script>
   </head>
<body onload="GetMap();">
<div id=’myMap’ style="position:relative; width:800px; height:600px;"></div>
<INPUT type="button" value="Find Locations" onclick="FindLocation(locations[0]);">
</body>
</html>

Advertisements

One thought on “Recursive Find Calls

  1. Fantastic.
    Spend too many time to find why my loop is behaving weired. And now i know how to deal with asyncronous call. or an alternative way to loop.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s