Find An Address: Locate is not a function

889
2
08-31-2012 07:59 AM
EmilyLaMunyon
Occasional Contributor
Hi,

I am trying to implement the "Find An Address" sample in my JavaScript map and keep getting an error in the Chrome console that reads "locate is not a function". I am using the ESRI locator in my code. Any ideas what I am doing wrong?

Thanks!

// Added for Locator Service    
 locator = new esri.tasks.Locator("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer");
         dojo.connect(locator, "onAddressToLocationsComplete", showResults);
   
   
   
    function locate() {
         map.graphics.clear();
         var address = {"SingleLineInput":dojo.byId("address").value};
         locator.outSpatialReference= map.spatialReference;
         var options = {
           address:address,
           outFields:["Loc_name"]
         }
         locator.addressToLocations(options);
       }
 
function showResults(candidates) {
         var candidate;
         var symbol = new esri.symbol.SimpleMarkerSymbol();
         var infoTemplate = new esri.InfoTemplate("Location", "Address: ${address}<br />Score: ${score}<br />Source locator: ${locatorName}");
 
        symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
         symbol.setColor(new dojo.Color([153,0,51,0.75]));
 
        var geom;
         
         dojo.every(candidates,function(candidate){
           console.log(candidate.score);
           if (candidate.score > 80) {
             console.log(candidate.location);
             var attributes = { address: candidate.address, score:candidate.score, locatorName:candidate.attributes.Loc_name };   
            geom = candidate.location;
             var graphic = new esri.Graphic(geom, symbol, attributes, infoTemplate);
             //add a graphic to the map at the geocoded location
             map.graphics.add(graphic);
             //add a text symbol to the map listing the location of the matched address.
             var displayText = candidate.address;
             var font = new esri.symbol.Font("16pt",esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL,esri.symbol.Font.WEIGHT_BOLD,"Helvetica");
            
            var textSymbol = new esri.symbol.TextSymbol(displayText,font,new dojo.Color("#666633"));
             textSymbol.setOffset(0,8);
             map.graphics.add(new esri.Graphic(geom, textSymbol));
             return false; //break out of loop after one candidate with score greater  than 80 is found.
           }
         });
         if(geom !== undefined){
           map.centerAndZoom(geom,12);
         }
 
      }
  
   


  
    <div dojoType="dijit.layout.ContentPane" id="addressPane" title="Find Address"> 
 Enter an input address and the application will use the sample address locator to return the location for 
        street addresses in the United States. 
        <br />
         <textarea type="text" id="address"/>2001 S. State Street, SLC, UT</textArea>
         <br />
         <button dojotype="dijit.form.Button" onclick="locate()"> Locate</button> 
      </div>
0 Kudos
2 Replies
KellyHutchins
Esri Frequent Contributor
It doesn't look like the locator you are using supports single line address input. You can test this using the rest services directory:

http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer/f...

Here's a modified version of your code using another locator that does support Single Line address input.
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Topographic Map</title>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.1/js/dojo/dijit/themes/claro/claro.css">
    <style>
      html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
      .esriScalebar{
        padding: 20px 20px;
      }
      #map{
        padding:0;
      }
    </style>
    <script type="text/javascript">var djConfig = {parseOnLoad: true};</script>
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.1"></script>
    <script type="text/javascript">
      dojo.require("dijit.layout.BorderContainer");
      dojo.require("dijit.layout.ContentPane");
      dojo.require("esri.map");

      var locator;
      var map;

      function init() {
         locator = new esri.tasks.Locator("http://tasks.arcgis.com/ArcGIS/rest/services/WorldLocator/GeocodeServer");
         dojo.connect(locator, "onAddressToLocationsComplete", showResults);
     
        var initExtent = new esri.geometry.Extent({"xmin":-122.46,"ymin":37.73,"xmax":-122.36,"ymax":37.77,"spatialReference":{"wkid":4326}});
        map = new esri.Map("map",{
          extent:esri.geometry.geographicToWebMercator(initExtent)
        });
        //Add the topographic layer to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service    
        var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
        map.addLayer(basemap);

        dojo.connect(map, 'onLoad', function(theMap) {
          //resize the map when the browser resizes
          dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
        });
      }

    function locate() {
        map.graphics.clear();
         var address = {"SingleLine":'380 New York St, Redlands, CA, 92373'};
         console.log(address);
         locator.outSpatialReference= map.spatialReference;
         var options = {
           address:address,
           outFields:["Loc_name"]
         }
         locator.addressToLocations(options);       
       }
function showResults(candidates) {
         var candidate;
         var symbol = new esri.symbol.SimpleMarkerSymbol();
         var infoTemplate = new esri.InfoTemplate("Location", "Address: ${address}<br />Score: ${score}<br />Source locator: ${locatorName}");
 
        symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
         symbol.setColor(new dojo.Color([153,0,51,0.75]));
 
        var geom;
         
         dojo.every(candidates,function(candidate){
           console.log(candidate.score);
           if (candidate.score > 80) {
             console.log(candidate.location);
             var attributes = { address: candidate.address, score:candidate.score, locatorName:candidate.attributes.Loc_name };   
            geom = candidate.location;
             var graphic = new esri.Graphic(geom, symbol, attributes, infoTemplate);
             //add a graphic to the map at the geocoded location
             map.graphics.add(graphic);
             //add a text symbol to the map listing the location of the matched address.
             var displayText = candidate.address;
             var font = new esri.symbol.Font("16pt",esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL,esri.symbol.Font.WEIGHT_BOLD,"Helvetica");
            
            var textSymbol = new esri.symbol.TextSymbol(displayText,font,new dojo.Color("#666633"));
             textSymbol.setOffset(0,8);
             map.graphics.add(new esri.Graphic(geom, textSymbol));
             return false; //break out of loop after one candidate with score greater  than 80 is found.
           }
         });
         if(geom !== undefined){
           map.centerAndZoom(geom,12);
         }
 
      }

      dojo.addOnLoad(init);
    </script>
  </head>
 
  <body class="claro">
    <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false" style="width: 100%; height: 100%; margin: 0;">
      <div dojotype='dijit.layout.ContentPane' region='top' style='height:40px;'>
        <button dojotype="dijit.form.Button" onclick="locate()"> Locate</button>
      </div> 
      <div id="map" dojotype="dijit.layout.ContentPane" region="center" style="border:1px solid #000;padding:0;">
     </div>
    </div>
  </body>

</html>


0 Kudos
EmilyLaMunyon
Occasional Contributor
Thanks Kelly!

I tried your suggestion, and I am still getting an error that reads: Locate is not a function. Is there something else I am missing?
 locator = new esri.tasks.Locator("http://tasks.arcgis.com/ArcGIS/rest/services/WorldLocator/GeocodeServer");
         dojo.connect(locator, "onAddressToLocationsComplete", showResults);
   
  function locate() {
         map.graphics.clear();
         var address = {"SingleLine":'1860 E Meadow Dr 84121'};
         locator.outSpatialReference= map.spatialReference;
         var options = {
           address:address,
           outFields:["Loc_name"]
         }
         locator.addressToLocations(options);
       }

0 Kudos