none
REST arguments Query callBack

    Question

  • Hello ,


    Please, I need help


    I call a Rest Service bing Maps for several addresses

    Adresses

    var adrs = [
    "  madrid espagne"
    ,"19  rue Frédéric Clavel 92150 SURESNES "
    ,"15  Allée des Mimosas VILLEFONTAINE 38090 "
    ,"12  chemin de Grave 69450 St Cyr au Mont d Or "
    ,"18  rue du bourg Tibourg 75014 PARIS "
    ,"Parc Valmy 52 Avenue FRANCOISE GIROUD 21000 DIJON France"
    ,"Résidence Le Cèdre316  avenue Anatole France 77190 DAMMARIE-LES-LYS "
    ,"9 ALLEE EVARISTE GALOIS 63170 AUBIERE France"
    ,"2  rue Barbusse WORLD TRADE CENTER 13001 MARSEILLE " ]

    Company Name

    var names = ["_testCGP","WL PATRIMOINE ","VALORIC CONSEIL PATRIMOINE","Metria Finance","HIPPARQUE","CCIF PROFESSIONS DE SANTE 21","VENDYSSEE ALAIN MALTAVERNE EIRL","ASALIA"]


    this is the asp.net code

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CarteCGP.aspx.cs" Inherits="WebApplicationCRMDynamicsBingMap.CarteCGP" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
       <title>Find a location by query</title>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
          <script runat="server">


    </script>
          <script type="text/javascript" src="js/JScript1.js"></script>
          <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
          <script type="text/javascript">
              var map = null, infobox, dataLayer;
              var query;
              var name;
              function getMap() {
             var mapOptions = {
             credentials: keyCode,
             center: new Microsoft.Maps.Location(47.19089, 2.57249),
             mapTypeId: Microsoft.Maps.MapTypeId.birdseye,
             zoom: 6,
             showScalebar: false
             }

         map = new Microsoft.Maps.Map(document.getElementById("myMap"), mapOptions);


         dataLayer = new Microsoft.Maps.EntityCollection();
         map.entities.push(dataLayer);

         var infoboxLayer = new Microsoft.Maps.EntityCollection();
         map.entities.push(infoboxLayer);

         infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), { visible: false, offset: new Microsoft.Maps.Point(0, 20) });
         infoboxLayer.push(infobox);

     }
                 function displayInfobox(e) {
                     if (e.targetType == 'pushpin') {
                         infobox.setLocation(e.target.getLocation());
                         infobox.setOptions({ visible: true, title: e.target.Title, description: e.target.Description });
                     }
                 }
              function findLocation() {
                  for (var i = 0; i < adrs.length; i++) {
                      query = adrs[i];
                      name = names[i];
                      map.getCredentials(callSearchService);
                  }
              }
              function callSearchService(credentials) {
                  var searchRequest = 'http://dev.virtualearth.net/REST/v1/Locations/' + query + '?output=json&jsonp=searchServiceCallback&key=' + credentials;
                  var mapscript = document.createElement('script');
                  mapscript.type = 'text/javascript';
                  mapscript.src = searchRequest;
                  document.getElementById('myMap').appendChild(mapscript)
              }
             
              function searchServiceCallback(result) {
                  var output = document.getElementById("output");
                  if (output) {
                      while (output.hasChildNodes()) {
                          output.removeChild(output.lastChild);
                      }
                  }
                  var resultsHeader = document.createElement("h5");
                  output.appendChild(resultsHeader);

                  if (result &&
              result.resourceSets &&
              result.resourceSets.length > 0 &&
              result.resourceSets[0].resources &&
              result.resourceSets[0].resources.length > 0) {
                      resultsHeader.innerHTML = "Bing Maps REST Search API  <br/>  Found location " + result.resourceSets[0].resources[0].name;
                      var bbox = result.resourceSets[0].resources[0].bbox;
                      var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0], bbox[1]), new Microsoft.Maps.Location(bbox[2], bbox[3]));
                      var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);
                      var pushpin = new Microsoft.Maps.Pushpin(location);
                      pushpin.Title = "Name : " + ??; <======== this is probleme i can
    not associate a name with the address;
                      pushpin.Description = "description :" + result.resourceSets[0].resources[0].name;
                      Microsoft.Maps.Events.addHandler(pushpin, 'click', displayInfobox);
                      map.entities.push(pushpin);
                  }
                  else {
                      nbm = nbm + 1;
                      if (typeof (response) == 'undefined' || response == null) {
                      }
                      else {
                          if (typeof (response) != 'undefined' && response && result && result.errorDetails) {
                              resultsHeader.innerHTML = "Message :" + response.errorDetails[0];
                          }
                      }
                  }
              }
          </script>
    </head>
    <body onload="getMap();">
        <form id="form1" runat="server" >
        <div>
            <div id='myMap' style=" width:100%; height:900px;"></div>
            <div>
                 <div id="Div1"></div>
                 <input type="button" value="FindLocation" onclick="findLocation();" />
            </div>
            <div id="output"></div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>


    Monday, July 15, 2013 12:45 PM

Answers

  • Ok, I understand what your issue is. Basically when you make a geocode request the call is made asynchronously and as a result multiple requests are made and returned but not necessarily in the same order.

    First off, do you really need to geocode the addresses when the application loads? Whenever possible you should geocode the addresses ahead of time and store the coordinates. This will make your application much faster and will also  significantly reduce the number of transactions your application creates.

    Secondly, you are using Bing Maps V7 which has a search module for Geocoding, so why are you using the REST services? http://msdn.microsoft.com/en-us/library/hh868062.aspx

    Now, back to your original issue. You can connect the response back to your originally data by passing simple string information in your requests using the jsono parameter in the REST request: http://msdn.microsoft.com/en-us/library/ff701701.aspx To tie the responses back to the original requests simply pass the array index as the jsono parameter. Change your callback to the following searchServiceCallback(result, index). The index will be the value you passed into the jsono parameter. Simply parse this value as an integer i.e. parseInt(index) and then use this with your array to get your original data.


    http://rbrundritt.wordpress.com

    Monday, July 15, 2013 2:10 PM
  • Ok, your code needs a lot of work. There is no need to loop through and call the map.getCredentials method. This makes everything slow for no reason. You only need to call that once when the app loads and store the credentials. Do the following:

    1. Add a global variable called sessionKey i.e. var sessionKey;
    2. Inside the getMap function add the following code:
      map.getCredentials(function(c){
             sessionKey = c;
      });
    3. Take the findLocation and the callSearchService functions and merge them together to create this:
    function findLocation() {
       for (var i = 0; i < adrs.length; i++) {
        query = adrs[i];
        name = names[i];
    
        var searchRequest = 'http://dev.virtualearth.net/REST/v1/Locations/' + query + '?output=json&jsonp=searchServiceCallback&jsono=' + i + '&key=' + sessionKey;
        var mapscript = document.createElement('script');
        mapscript.type = 'text/javascript';
        mapscript.src = searchRequest;
        document.getElementById('myMap').appendChild(mapscript)
     }
    }
    

    4. Change this line of code:
    function searchServiceCallback(result) {
    to this:
    function searchServiceCallback(result, index) {

    5. Finally you can get the name like this:
    pushpin.Title = "Name : " + names[parseInt(index)];


    http://rbrundritt.wordpress.com

    Monday, July 15, 2013 5:48 PM

All replies

  • Ok, I understand what your issue is. Basically when you make a geocode request the call is made asynchronously and as a result multiple requests are made and returned but not necessarily in the same order.

    First off, do you really need to geocode the addresses when the application loads? Whenever possible you should geocode the addresses ahead of time and store the coordinates. This will make your application much faster and will also  significantly reduce the number of transactions your application creates.

    Secondly, you are using Bing Maps V7 which has a search module for Geocoding, so why are you using the REST services? http://msdn.microsoft.com/en-us/library/hh868062.aspx

    Now, back to your original issue. You can connect the response back to your originally data by passing simple string information in your requests using the jsono parameter in the REST request: http://msdn.microsoft.com/en-us/library/ff701701.aspx To tie the responses back to the original requests simply pass the array index as the jsono parameter. Change your callback to the following searchServiceCallback(result, index). The index will be the value you passed into the jsono parameter. Simply parse this value as an integer i.e. parseInt(index) and then use this with your array to get your original data.


    http://rbrundritt.wordpress.com

    Monday, July 15, 2013 2:10 PM
  • Yes thank you

    I understand whatyou mean

    but
    how to use the json argument in my code "jsonso"

    var searchRequest = 'http://dev.virtualearth.net/REST/v1/Locations/' + query + '?output=json&jsonp=searchServiceCallback&jsonso='+name+'&key=' + credentials;

    thenI have to dowhat

    thanks
    Monday, July 15, 2013 3:23 PM
  • Ok, your code needs a lot of work. There is no need to loop through and call the map.getCredentials method. This makes everything slow for no reason. You only need to call that once when the app loads and store the credentials. Do the following:

    1. Add a global variable called sessionKey i.e. var sessionKey;
    2. Inside the getMap function add the following code:
      map.getCredentials(function(c){
             sessionKey = c;
      });
    3. Take the findLocation and the callSearchService functions and merge them together to create this:
    function findLocation() {
       for (var i = 0; i < adrs.length; i++) {
        query = adrs[i];
        name = names[i];
    
        var searchRequest = 'http://dev.virtualearth.net/REST/v1/Locations/' + query + '?output=json&jsonp=searchServiceCallback&jsono=' + i + '&key=' + sessionKey;
        var mapscript = document.createElement('script');
        mapscript.type = 'text/javascript';
        mapscript.src = searchRequest;
        document.getElementById('myMap').appendChild(mapscript)
     }
    }
    

    4. Change this line of code:
    function searchServiceCallback(result) {
    to this:
    function searchServiceCallback(result, index) {

    5. Finally you can get the name like this:
    pushpin.Title = "Name : " + names[parseInt(index)];


    http://rbrundritt.wordpress.com

    Monday, July 15, 2013 5:48 PM
  • Hi Ricky

    This was really useful for myself and the JSONO callback parameter is exactly what I need to be able to geocode a set of data, and put a label on each pin using that original data.

    BUT...when I run my code I always get an "undefined" value for the returned index in the call back function.

    Any ideas??

    Thanks

    Andrew

    Tuesday, November 19, 2013 10:45 PM