none
Bing Maps REST Services-Routes API : How to get response with auto correct address? RRS feed

  • Question

  • Hi All,

    I am trying to build a driving route using Bing Maps REST Services-Routes API. I am using following code:

    start ='8351 Rockville Rd Indianapolis IN 46234'; end = '5th St Nw & Glenburn Rd Linton IN 47441';

    var routeRequest = 'http://dev.virtualearth.net/REST/v1/Routes?wp.0=' + start + '&wp.1=' + end + '&routePathOutput=Points&output=json&jsonp=RouteCallback&key=' + credentials;

    Getting response with status code 404-'Not Found' (because of the end address - '5th St Nw & Glenburn Rd Linton IN 47441').

    Is there any parameter which I can set in request and it returns auto correct address in callback instead of status code-404?

    When I tried to get direction on BingMap site with the same address(start,end), it shows route on MAP with auto corrected address.

    • Given address :- '5th St Nw & Glenburn Rd Linton IN 47441'
    • BingMap site updated address: - ‘5th St NW & Dickson St, Linton, IN 47441’

    I need same thing from Bing Maps REST Services-Routes API. Is there any way?

    Thanks,

    Vina.

    Thursday, January 17, 2013 7:35 AM

Answers

  • You need to wrap the start and end with the encodeUri method, not the full URL.

    http://rbrundritt.wordpress.com

    • Marked as answer by Vina_S Monday, January 21, 2013 5:07 AM
    Thursday, January 17, 2013 2:18 PM

All replies

  • The issue with your request is the ampersand (&). In the query string of the URL this is used to indicate a new query string parameter. You can support queries like this by encode/escaping the queries. In JavaScript wrap with encodeURI(query), in .NET you can use Uri.EscapeDataString(query).

    As for the Dickenson/Glenburn confusion, the road in question alternates between the two names. this can be seen on the map.


    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Thursday, January 17, 2013 10:49 AM
    Thursday, January 17, 2013 10:49 AM
  • Thanks for replying Richard_Brundritt

    I have already used encodeURI. See the 'callRouteService' function in following code where I have used encodeURI function.

    But getting response with status code 404-'Not Found'.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
        <title>Find directions</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 = null;
            var end;
            var start;
            function getMap() {
                map = new Microsoft.Maps.Map(document.getElementById('myMap'), { credentials: 'BingMap Key' });
                 alert('Insert key to make valid REST service call');
            }
    
            function getDirections() {
                start = '8351 Rockville Rd Indianapolis IN 46234'; end = '5th St Nw & Glenburn Rd Linton IN 47441';
                map.getCredentials(callRouteService);
            }
    
            function callRouteService(credentials) {            
                var routeRequest = 'http://dev.virtualearth.net/REST/v1/Routes?wp.0=' + start + '&wp.1=' + end + '&routePathOutput=Points&output=json&jsonp=routeCallback&key=' + credentials;
                var mapscript = document.createElement('script');
                mapscript.type = 'text/javascript';
                mapscript.src = encodeURI(routeRequest);
                document.getElementById('myMap').appendChild(mapscript);
            }
    
         function routeCallback(result) {
    	 
    	 }            
           
          </script>
    </head>
    <body onload="getMap();">
        <div id='myMap' style="; width: 400px; height: 400px;">
        </div>
        <div>
            <input type="button" value="GetDirections" onclick="getDirections();" />
        </div>
        <div id="output">
        </div>
    </body>
    </html>

    Thursday, January 17, 2013 1:37 PM
  • You need to wrap the start and end with the encodeUri method, not the full URL.

    http://rbrundritt.wordpress.com

    • Marked as answer by Vina_S Monday, January 21, 2013 5:07 AM
    Thursday, January 17, 2013 2:18 PM
  • Issue was with encodeURI. After some R&D I have found 'encodeURI()' will not encode: ~!@#$&*()=:/,;?+'. So I have used escape() for encode given string. [escape()will not encode: @*/+].

    It is working correctly now.

    Monday, January 21, 2013 5:06 AM