Select to view content in your preferred language

Using Locate to geocode address

1029
4
07-06-2011 07:40 AM
DavidBrown6
Emerging Contributor
Hi,

I am trying to geocode an address using the locate task, and I keep getting the error "Address or Intersection must be specified"

As far as I can tell, I am using the correct model for the addressToLocations() functions.

Here is what I have:

function init() {
        locator = new esri.tasks.Locator("http://gis.hamiltoncounty.in.gov/ArcGIS/rest/services/Addresses/GeocodeServer");
  dojo.connect(locator, "onAddressToLocationsComplete");

function locate() {
  var add = dojo.byId("address").value.split(",");
  var address = {
    Street : add[0],
    City : add[1],
    State : add[2],
    Zip: add[3]
   };
  locator.addressToLocations(address, ["Loc_name"]);
      }
 
       dojo.addOnLoad(init);
    </SCRIPT>

</head>
  <body class="claro">
    Find Address by Location: <input type="text" id="address" size="40" value="1 Civic Sq, Carmel, IN, 46032" /> <i>(Street, City, State, Zip)</i>
    <input type="button" value="Locate" onclick="locate()" />
    <div id="tbl"></div>
  </body>
</html>
0 Kudos
4 Replies
MarkHoover
Frequent Contributor
Two quick thoughts:

dojo.connect(locator, "onAddressToLocationsComplete");

Doesn't this require a callback?

i.e.

dojo.connect(locator, "onAddressToLocationsComplete", processResults);

function processResults(addressCandidates)
{
     //handle your processing here, i.e. zoom to, etc.
}

Also, in the API it lists both of these formats for the address:

{
  Street: "<street>",
  City: "<city>",
  State: "<state>",
  Zone: "<zone>"
}

{
    street: "380 New York",
    city: "Redlands",
    state: "CA",
    zip: "92373"
  }

(Note the difference in captialization...dunno if it bothers it or not, but it's a discrepancy I saw)
0 Kudos
HemingZhu
Frequent Contributor
Two quick thoughts:

dojo.connect(locator, "onAddressToLocationsComplete");

Doesn't this require a callback?

i.e.

dojo.connect(locator, "onAddressToLocationsComplete", processResults);

function processResults(addressCandidates)
{
     //handle your processing here, i.e. zoom to, etc.
}

Also, in the API it lists both of these formats for the address:

{
  Street: "<street>",
  City: "<city>",
  State: "<state>",
  Zone: "<zone>"
}

{
    street: "380 New York",
    city: "Redlands",
    state: "CA",
    zip: "92373"
  }

(Note the difference in captialization...dunno if it bothers it or not, but it's a discrepancy I saw)

Besides what Mark pointed out, looking at your locator: http://gis.hamiltoncounty.in.gov/ArcGIS/rest/services/Addresses/GeocodeServer. There are two address fields:FULL_ADDRESS and CITY. You need specify them (CITY optional). This is what your code should looks:
function init() {
....
locator = new esri.tasks.Locator("http://gis.hamiltoncounty.in.gov/ArcGIS/rest/services/Addresses/GeocodeServer");
dojo.connect(locator, "onAddressToLocationsComplete",  processResults);
var address ={}; //or var address =new Object();
address.FULL_ADDRESS =FullAddressString;
address.CITY =CityString;  //optional
locator.addressToLocations(address);
....
}
 
function processResults(addressCandidates)
{
//handle your processing here, i.e. zoom to, etc.
}

0 Kudos
SaurabhGupta
Emerging Contributor
The argument for addressToLocations should always be the Address Fields for the GeocodeService that you are using.
http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi_start.htm#jsapi/locator.htm

In your case they are : FULL_ADDRESS and CITY.

Thus, as pointed above, your call should be like :

var address = {
   FULL_ADDRESS : "380 New York St.",
    CITY: "Redlands"
  };
  locator.addressToLocations(address);
0 Kudos
DavidBrown6
Emerging Contributor
Thanks guys I appreciate your help, it is running now. Apparently I needed to specify the CITY attribute as well, even though on the server itself it is listed as optional (and you don't need it to run the find directly on the geocodeServer).
0 Kudos