Select to view content in your preferred language

catch no result in custom geocoder

459
2
Jump to solution
05-08-2014 11:48 AM
MarcusTownsend
Deactivated User
I'm new to the Javascript API but not new to Javascript, but maybe rusty admittedly.

I'm having a hard time figuring out how to catch when a custom geocoder does not return an address for entered search.

Example coded below.

Search for "hay" and the display div is populated with the returned name "Hay street". if you search for "super street" , which returns nothing. I would want it display "no information"


Anyone able to guide me in the right direction.


<!DOCTYPE html> <html>   <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">     <title></title>          <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">     <style>       html, body, #map {         height: 100%;         width: 100%;         margin: 0;         padding: 0;       }       #search {         display: block;         position: absolute;         z-index: 3;         top: 20px;         left: 75px;       }       #display {        padding-top:200px;       }     </style>          <script src="http://js.arcgis.com/3.9/"></script>     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>     <script>       require([         "esri/map",         "esri/dijit/Geocoder",          "dojo/dom",         "dojo/domReady!"       ], function(         Map, Geocoder,         dom       ) {          // create a map and instance of the geocoder widget here         var myGeocoders = [{           url: "http://gis.ci.fayetteville.nc.us/arcgis/rest/services/Composite_Locator/GeocodeServer",           name: "Composite_Locator",           singleLineFieldName: "Street"         }];                              var geocoder =  new Geocoder({           autoComplete: true,           arcgisGeocoder: false,           geocoders: myGeocoders         }, dom.byId("search"));        //  map.on("load", enableSpotlight);          geocoder.on("select", showLocation);       //  geocoder.on("clear", removeSpotlight);          function showLocation(evt) {         // console.log(evt)           $('#display').append(evt.result.name);         //  console.log(evt.result.name)         }        });     </script>   </head>   <body class="soria">     <div id="search"></div>     <div id="display"></div>               </body>  </html>
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Marcus,

You can accomplish this by using the find-results event.  Ex:

geocoder.on('find-results', showLocation)  function showLocation(evt) {   if(evt.results.results.length > 0){     $('#display').append(evt.results.results[0].name);   }   else{     $('#display').append("No Information");   } }

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Marcus,

You can accomplish this by using the find-results event.  Ex:

geocoder.on('find-results', showLocation)  function showLocation(evt) {   if(evt.results.results.length > 0){     $('#display').append(evt.results.results[0].name);   }   else{     $('#display').append("No Information");   } }
0 Kudos
MarcusTownsend
Deactivated User
Thanks for the response. I thought that 'find-results' would only happen when results where where returned based off the api summery.

Worked like charm in my app.
0 Kudos