Locator - input address, append my state if the user doesn't enter one?

946
4
Jump to solution
12-03-2012 07:17 AM
TracySchloss
Frequent Contributor
I am using the single line World Locator.  I like that it works for both either a full address (310 W High St, Jefferson City, MO 65101) or just a city name (Jefferson City, MO).  Since I am working with data specific to Missouri, I've been asked to make this more user friendly and somehow programmatically manage adding MO at the end of a city name if the user doesn't specify.

I didn't think this would be a problem.  I have an inputText field, textAddress and I'm defining the input address as the value in it.
    var inputAddress = {         "SingleLine" : dojo.byId("txtAddress").value };


In Firebug, it says inputAddress is [object Object], but I can examine the value of SingleLine and see what I entered.

Next I check to see if inputAddress already has the Missouri prefix, MO, at the end of the string:
var checkMO = inputAddress.SingleLine.slice(-2).toUpperCase();  if ( checkMO !== 'MO' ) {                 console.log("not MO");                 inputAddress = inputAddress.SingleLine + ", MO";                 console.log(inputAddress);             }else {                 console.log(inputAddress.SingleLine + " has MO");                            }

Then I pass inputAddress to the locator:
locator.outSpatialReference = map.spatialReference; var options = {     address : inputAddress,     outFields : ["*"]     };     locator.addressToLocations(inputAddress); }


This doesn't work.  I think it is because inputAddress starts out as an object and I'm trying to deal with a string.  I tried  inputAddress + ", MO" instead of inputAddress.SingleLine + ", MO", but neither seems to work.  Instead the locator says it found no matches.  I can see that the inputAddress looks OK in the console window. 

Could someone explain what's wrong with my logic and/or provide a workaround to this?
0 Kudos
1 Solution

Accepted Solutions
TracySchloss
Frequent Contributor
This turned out to be a syntax problem in how I was formatting the address.  The original address was defined as     var
inputAddress = {         "SingleLine" : dojo.byId("txtAddress").value };


When I was creating the new input address, I was neglecting the "SingleLine" : part of this, which was making it properly formatted.  I also needed to take into account a fully entered address, complete with ZIP code that might be used in a single line address input.

This is what I came up with and I think will satisfy my situation.  I don't have 10.1 server installed, so anything based on that is not an option.
function locate() {     map.graphics.clear();     locator.outSpatialReference = map.spatialReference;     var inputAddress = {         "SingleLine" : dojo.byId("txtAddress").value };  //    console.log(address.SingleLine);             var checkMO = inputAddress.SingleLine.slice(-2).toUpperCase();             var isNotANumber = false;             if (isNaN(checkMO)){                 isNotANumber = true;                              }             var options;             if ( checkMO !== 'MO' && isNotANumber ) {               //  console.log("not MO");                 var newInputAddress = {"SingleLine" : dojo.byId("txtAddress").value + ",MO"};                 options = {                 address : newInputAddress,                 outFields : ["*"]                 };                 locator.addressToLocations(newInputAddress);             }else {               //  console.log(inputAddress.SingleLine + "has MO");                 options = {                 address : inputAddress,                 outFields : ["*"]             };             locator.addressToLocations(inputAddress);             }                      }

View solution in original post

0 Kudos
4 Replies
StephenLead
Regular Contributor III

Next I check to see if inputAddress already has the Missouri prefix, MO, at the end of the string:
var checkMO = inputAddress.SingleLine.slice(-2).toUpperCase();
 if ( checkMO !== 'MO' ) {
                console.log("not MO");
                inputAddress = inputAddress.SingleLine + ", MO";
                console.log(inputAddress);
            }else {
                console.log(inputAddress.SingleLine + " has MO");
              
            }



Before you pass inputAddress to the locator, what value is shown in your console.log(inputAddress) call above?

Steve
0 Kudos
GeorgeSimpson
Occasional Contributor
GeocodeServers at 10.1 include a "SearchExtent" parameter that might be easier for you to implement.  I'm playing around with some locators now and will probably attempt to limit the results to my state as well.  When I get to that part, I'll let you know how it works
0 Kudos
StephenLead
Regular Contributor III
...programmatically manage adding MO at the end of a city name *if the user doesn't specify*.


George, note that this should default to MO, but still allow the user to search anywhere, so restricting the search may not be optimal.

Cheers,
Steve
0 Kudos
TracySchloss
Frequent Contributor
This turned out to be a syntax problem in how I was formatting the address.  The original address was defined as     var
inputAddress = {         "SingleLine" : dojo.byId("txtAddress").value };


When I was creating the new input address, I was neglecting the "SingleLine" : part of this, which was making it properly formatted.  I also needed to take into account a fully entered address, complete with ZIP code that might be used in a single line address input.

This is what I came up with and I think will satisfy my situation.  I don't have 10.1 server installed, so anything based on that is not an option.
function locate() {     map.graphics.clear();     locator.outSpatialReference = map.spatialReference;     var inputAddress = {         "SingleLine" : dojo.byId("txtAddress").value };  //    console.log(address.SingleLine);             var checkMO = inputAddress.SingleLine.slice(-2).toUpperCase();             var isNotANumber = false;             if (isNaN(checkMO)){                 isNotANumber = true;                              }             var options;             if ( checkMO !== 'MO' && isNotANumber ) {               //  console.log("not MO");                 var newInputAddress = {"SingleLine" : dojo.byId("txtAddress").value + ",MO"};                 options = {                 address : newInputAddress,                 outFields : ["*"]                 };                 locator.addressToLocations(newInputAddress);             }else {               //  console.log(inputAddress.SingleLine + "has MO");                 options = {                 address : inputAddress,                 outFields : ["*"]             };             locator.addressToLocations(inputAddress);             }                      }
0 Kudos