none
Bing Map AJAX API 7 - How to find out which address is invalid in request sent to directionsManager.calculateDirections method in case of error? RRS feed

  • Question

  • Hi,

    I am using Bing Map 7 AJAX API's directionsManager.calculateDirections function to get Route. For few routes(e.g. "Irving, TX" to "Freeport Pkwy") Bing returns error in response ("We didn't find results for your search"). So here how can we identify which address is invalid out of those two?

    e.g. see below example of www.bing.com/Maps, it show error message in front of invalid address textbox only:


    Wednesday, August 6, 2014 10:59 AM

Answers

  • This was an interesting one that I hadn't been asked before. Looking in the documentation I found this: http://msdn.microsoft.com/en-us/library/hh312834.aspx 

    However, I noticed that if the render option "autoDisplayDisambiguation" is set to true, which is the default value, the directions error event is never fired and you can't catch this disambiguation error. Having it auto display the disambiguous information is nice so I looked into how we can figure out when no disambiguous result is found for a waypoint. I figured out that the "beforeDisambiguationRender" event gets fired when in this case and we can use this to figure out which waypoint has the issue. Funny enough this event doesn't fire when the "autoDisplayDisambiguation" value is set to false, which is the complete opposite issue from before. So, with this in mind I combined both of these solutions together so that it doesn't matter what the value of "autoDisplayDisambiguation" is. I also added some logic to figure out the index of the waypoint in the directions manager waypoint array and also calculated the number of suggested alternatives. If the number of alternatives is 0 then this fits the case you mentioned, if it is not 0 then standard disambiguation would apply. Here is the code I put together:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
       <head>
    		<title></title>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    		<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
    		<script type="text/javascript">
    		var map;
    		var directionsManager;
    					  
    		function getMap()
    		{
    			map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: 'YOUR_BING_MAPS_KEY'});
    			Microsoft.Maps.loadModule('Microsoft.Maps.Directions');
    		}
          
    		function createDirectionsManager()
    		{
    			if (!directionsManager) 
    			{
    				directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
    
    				Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', function(e) { 
    					if (e.responseCode == Microsoft.Maps.Directions.RouteResponseCode.waypointDisambiguation)
    					{
    						DisplayDisambiguation(e);
    					}
    				});
    
    				Microsoft.Maps.Events.addHandler(directionsManager, 'beforeDisambiguationRender', DisplayDisambiguation);
    			}
    		}
    		  
    		function calculateDirections()
    		{
    			if (!directionsManager) { 
    				createDirectionsManager(); 
    			}
    
    			directionsManager.resetDirections();
    
    			directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
    
    			directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsItinerary') });
    
    			directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'Seattle, WA' }));
    
    			directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'notAValidAddress' }));
    
    			directionsManager.calculateDirections();
    		}
    		  
    		function DisplayDisambiguation(arg)
    		{		
    			var waypointIdx = -1;
    			var waypoints = directionsManager.getAllWaypoints();
    			
    			for(var i=0;i< waypoints.length;i++){
    				if(waypoints[i].getAddress() == arg.waypoint.getAddress()){
    					waypointIdx = i;
    					break;
    				}
    			}
    			
    			var disambiguousResult = arg.waypoint.getDisambiguationResult();
    			var numResults = disambiguousResult.businessSuggestions.length + disambiguousResult.locationSuggestions.length;
    			
    			alert("Unable to find waypoint: '" + arg.waypoint.getAddress() + "' which is waypoint index: " + waypointIdx + ". There are " + numResults + " suggestions.");
    		}
          </script>
       </head>
     <body onload="getMap();">
          <div id='myMap' style="; width:400px; height:400px;"></div>
          <div>
             <input type="button" value="Calculate Directions" onclick="calculateDirections();" />
          </div>
          <div id='directionsItinerary'> </div> 
       </body>
    </html>
    

    http://rbrundritt.wordpress.com

    Wednesday, August 6, 2014 1:28 PM

All replies

  • Does anyone have answer for the above question? Is www.bing.com/Maps doing address validation(geocoding) for each textbox on their onchange event?
    Wednesday, August 6, 2014 12:52 PM
  • This was an interesting one that I hadn't been asked before. Looking in the documentation I found this: http://msdn.microsoft.com/en-us/library/hh312834.aspx 

    However, I noticed that if the render option "autoDisplayDisambiguation" is set to true, which is the default value, the directions error event is never fired and you can't catch this disambiguation error. Having it auto display the disambiguous information is nice so I looked into how we can figure out when no disambiguous result is found for a waypoint. I figured out that the "beforeDisambiguationRender" event gets fired when in this case and we can use this to figure out which waypoint has the issue. Funny enough this event doesn't fire when the "autoDisplayDisambiguation" value is set to false, which is the complete opposite issue from before. So, with this in mind I combined both of these solutions together so that it doesn't matter what the value of "autoDisplayDisambiguation" is. I also added some logic to figure out the index of the waypoint in the directions manager waypoint array and also calculated the number of suggested alternatives. If the number of alternatives is 0 then this fits the case you mentioned, if it is not 0 then standard disambiguation would apply. Here is the code I put together:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
       <head>
    		<title></title>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    		<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
    		<script type="text/javascript">
    		var map;
    		var directionsManager;
    					  
    		function getMap()
    		{
    			map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: 'YOUR_BING_MAPS_KEY'});
    			Microsoft.Maps.loadModule('Microsoft.Maps.Directions');
    		}
          
    		function createDirectionsManager()
    		{
    			if (!directionsManager) 
    			{
    				directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
    
    				Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', function(e) { 
    					if (e.responseCode == Microsoft.Maps.Directions.RouteResponseCode.waypointDisambiguation)
    					{
    						DisplayDisambiguation(e);
    					}
    				});
    
    				Microsoft.Maps.Events.addHandler(directionsManager, 'beforeDisambiguationRender', DisplayDisambiguation);
    			}
    		}
    		  
    		function calculateDirections()
    		{
    			if (!directionsManager) { 
    				createDirectionsManager(); 
    			}
    
    			directionsManager.resetDirections();
    
    			directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
    
    			directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsItinerary') });
    
    			directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'Seattle, WA' }));
    
    			directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'notAValidAddress' }));
    
    			directionsManager.calculateDirections();
    		}
    		  
    		function DisplayDisambiguation(arg)
    		{		
    			var waypointIdx = -1;
    			var waypoints = directionsManager.getAllWaypoints();
    			
    			for(var i=0;i< waypoints.length;i++){
    				if(waypoints[i].getAddress() == arg.waypoint.getAddress()){
    					waypointIdx = i;
    					break;
    				}
    			}
    			
    			var disambiguousResult = arg.waypoint.getDisambiguationResult();
    			var numResults = disambiguousResult.businessSuggestions.length + disambiguousResult.locationSuggestions.length;
    			
    			alert("Unable to find waypoint: '" + arg.waypoint.getAddress() + "' which is waypoint index: " + waypointIdx + ". There are " + numResults + " suggestions.");
    		}
          </script>
       </head>
     <body onload="getMap();">
          <div id='myMap' style="; width:400px; height:400px;"></div>
          <div>
             <input type="button" value="Calculate Directions" onclick="calculateDirections();" />
          </div>
          <div id='directionsItinerary'> </div> 
       </body>
    </html>
    

    http://rbrundritt.wordpress.com

    Wednesday, August 6, 2014 1:28 PM
  • Excellent!

    Thank you!

    Thursday, August 7, 2014 6:51 AM