0

Working with this example from Bing Maps V8

This line kicks off the map search: geocodeQuery("New York, NY");

This code fragment contains the callback function (which places the pin on the map):

var searchRequest = {
  where: query,
  callback: function (r) {
    //Add the first result to the map and zoom into it.
    if (r && r.results && r.results.length > 0) {
      var pin = new Microsoft.Maps.Pushpin(r.results[0].location);
      map.entities.push(pin);
      map.setView({ bounds: r.results[0].bestView });
    }
  },
  errorCallback: function (e) {
    //If there is an error, alert the user about it.
    alert("No results found.");
  }
};

If I want to execute multiple searches, I can write:

geocodeQuery("New York, NY");
geocodeQuery("Queens, NY");

Now I have 2 pins on the map.

Next step: if I want to label the pins I'd extend the 'new pushpin code':

var pin = new Microsoft.Maps.Pushpin(r.results[0].location,
{
    title: 'Queens',
    text: '2'
});

Question:

The pins are placed by the callback function. Since I want each pin to have different text, how do I tweak this sample code so that I can pass new parameters to the callback function?

Yannick K
  • 4,887
  • 3
  • 11
  • 21
Norsak
  • 181
  • 1
  • 5
  • Does this answer your question? [JavaScript: Passing parameters to a callback function](https://stackoverflow.com/questions/3458553/javascript-passing-parameters-to-a-callback-function) – Heretic Monkey Nov 04 '19 at 16:07

1 Answers1

0

You would need to add an argument to the geocodeQuery function (maybe called title) and use that when you create the pin in the callback. Then you could simply call geocodeQuery("Queens, NY", "Queens")

function geocodeQuery(query, title) {
    //If search manager is not defined, load the search module.
    if (!searchManager) {
        //Create an instance of the search manager and call the geocodeQuery function again.
        Microsoft.Maps.loadModule('Microsoft.Maps.Search', function () {
            searchManager = new Microsoft.Maps.Search.SearchManager(map);
            geocodeQuery(query, title);
        });
    } else {
        var searchRequest = {
            where: query,
            callback: function (r) {
                //Add the first result to the map and zoom into it.
                if (r && r.results && r.results.length > 0) {
                    var pin = new Microsoft.Maps.Pushpin(r.results[0].location, {
                        title: title,
                        text: '2'
                    });
                    map.entities.push(pin);
                    map.setView({ bounds: r.results[0].bestView });
                }
            },
            errorCallback: function (e) {
                //If there is an error, alert the user about it.
                alert("No results found.");
            }
        };
        //Make the geocode request.
        searchManager.geocode(searchRequest);
    }
}
JstnPwll
  • 8,585
  • 2
  • 33
  • 56
  • ...except that doesn't work. I can pass the parameter the geocodeQuery, that works, but the callback still considers 'title' undefined. I cut'n paste your code just to make sure I didn't miss anything subtle – Norsak Nov 04 '19 at 16:24
  • @Norsak You also have to pass the title into the inner `geocodeQuery` call (which happens if the searchManager is undefined). I didn't catch that the first time, and the goal isn't to provide perfect code for a copy-paste but for you to understand the principle and make the necessary changes yourself. – JstnPwll Nov 04 '19 at 16:28
  • 1
    THAT's what it was!!! It's that weird little recursive self-call, that was stripping away the additional parameter(s). I had tried several different approaches, half of them probably would have worked if i had remembered the inner `geocodeQuery` call ! Thank you for the second pair of eyes, you have solved my query, turns out the challenge is not passing params to callback, but reading someone else's code more carefully, – Norsak Nov 04 '19 at 16:36
  • Great! Glad we caught that – JstnPwll Nov 04 '19 at 16:43