address-to-locations-complete runs callback function repeatedly

2278
2
Jump to solution
09-09-2015 10:54 AM
AzizaParveen2
New Contributor III

Hi, I am using address-to-locations-complete event with our custom locator, now if I type wrong address or garbage address, and click find button, the locator task runs just fine and brought 0 result and then show an alert msg saying "Address not Found", first time it works fine but I input wrong address or garbage address again, the alert box comes up twice, and this goes on and on..which is very annoying, did any of you see this behavior? I will appreciate any help in this regard, thanks--AZIZA

I made a very simple bare bone app with only few lines of code in it, I am pretty sure you guys can replicate this issue.

var searchByAddress, showResultLocation;

require([

          

         "esri/tasks/locator",

         "dojo/domReady!"

], function (locator) {

   

    esriConfig.defaults.io.alwaysUseProxy = false;

    esriConfig.defaults.io.corsDetection = false;

    esriConfig.defaults.io.corsEnabledServers.push("http://services.arcgisonline.com/");

    esriConfig.defaults.io.corsEnabledServers.push("http://www.arcgis.com/");

    var rvcLocator = new locator(locator);

    searchByAddress = function () {

        var address = {

            SingleLine: document.getElementById("search").value

        };

        var options = {

            address: address,

            outFields: ["*"]

        };

        showResultLocation = function (evt) {

            var resultLocations = evt.addresses.length;

            if (resultLocations == 0) {

                alert("No Address found");

                return;

            }

        }

        rvcLocator.on("address-to-locations-complete", showResultLocation);

        rvcLocator.addressToLocations(options);

    };

   

});

HTML

<!DOCTYPE html>

<html>

<head>

  

   <title>ArcGIS API for JavaScript | Basic Search</title>

   <link rel="stylesheet" href="http://js.arcgis.com/3.14/dijit/themes/claro/claro.css">

   <link rel="stylesheet" href="http://js.arcgis.com/3.14/esri/css/esri.css">

   <style>

      html,

      body,

     

      #search {

         display: block;

         position: absolute;

         z-index: 2;

         top: 20px;

         left: 74px;

      }

   </style>

   <script src="http://js.arcgis.com/3.14/"></script>

   <script src="JS/test.js"></script>

  

</head>

<body>

   <input id ="search" type="text"/>

   <input id="searchAddBtn" type="button" value="FIND" onclick="searchByAddress();"/>

</body>

</html>

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Aziza,

   The reason is because you are adding the "address-to-locations-complete" function each time you run the search. So each time you search you are adding it again and thus the multiple alerts.

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Aziza,

   The reason is because you are adding the "address-to-locations-complete" function each time you run the search. So each time you search you are adding it again and thus the multiple alerts.

0 Kudos
AzizaParveen2
New Contributor III

Thank you so much Robert, it worked

0 Kudos