Geocoder custom service not working

3121
14
09-06-2013 11:01 AM
PLadd
by
Occasional Contributor III
I'm having trouble using my own geocoding service and hope someone can set me on the right path. 

I've looked at the ESRI sample and at the sample at the bottom of this page but am not able to insert my own geocoder custom service url and name and get it to work.

I ran it through jsfiddle but get no errors.  It just does nothing.  If it helps, my geocoding service is located here. and name is MeridenGeocoder.

Does my spatial reference matter?  My geocoder service is working against a street centerline that is CT stateplane, NAD83 feet (Spatial Reference: 102656  (2234) ).  Thanks for your consideration in this matter.
14 Replies
JohnGravois
Frequent Contributor
your geocoding service is definitely able to return candidates with web mercator coordinates.  i was able to get your locator returning results instead of errors by telling the geocoder object constructor that the name of your single line input parameter field is "Street" instead of "Single Line Input".

var myGeocoders = [{
          url: locatorUrl,
          name: "Meriden",
   //if your locator uses a single line input field name other than 'Single Line Input',
          //tell the widget how to make the appropriate request
          singleLineFieldName: "Street"
}];
geocoder = new Geocoder({
          map: map,
          autocomplete: true,
          arcgisGeocoder: false,
          geocoders: myGeocoders,
          value: "Main St."
},"search");


check out this forum thread for more info.
0 Kudos
PLadd
by
Occasional Contributor III
Thanks John.  With your help, I now see that my customized geocoder was working all along.  The real issue is that it's not zooming to the location.  So when I enter the address for City Hall, while zoomed out to full extent, nothing would happen.  If I zoomed in to some area other than City Hall and then entered it's address, the map would zoom to that location. 

Is there a way to get the goecoder to zoom?  I see I can do it with this locator sample but that seems to require an awful lot of scripting, which I'm trying to avoid.  When I use this geocoder sample, it seems to be zooming.  What makes mine different?

Here's what I've got so far:
var gc = [{
                url: "http://gis1.meridenct.gov/arcgis/rest/services/MeridenGeocoder/GeocodeServer",
                name: "MeridenGeocoder",
                singleLineFieldName: "Street"
            }];
            var geocoder = new esri.dijit.Geocoder({
                map: map,
                autoNavigate: true,
                autoComplete: true,
                geocoders: gc,
                geocoderMenu: false,
                arcgisGeocoder: false,
                placeholder: "142 East Main St"
            }, "search");
            geocoder.startup();
0 Kudos
JohnGravois
Frequent Contributor
our own geocoding service returns an "extent" along with the candidate point geometry.  if your own service does not, it would probably be easiest to call map.centerAndZoom() like in this sample.
0 Kudos
PLadd
by
Occasional Contributor III
I've been playing in the sandbox with this sample, but I can't seem to make it work on my geocode service. 

I changed the locator url to my service but I can't be sure what else I should be changing to make it work.  Can you provide some guidance?

locator = new Locator("http://gis1.meridenct.gov/arcgis/rest/services/MeridenGeocoder/GeocodeServer");

Should this
"SingleLine": dom.byId("address").value
be this
"SingleLineFieldName": dom.byId("address").value

And these lines I've tried changing to other values but no luck
outFields: ["Loc_name"]
"Address: ${address}<br />Score: ${score}<br />Source locator: ${locatorName}"
0 Kudos
JohnGravois
Frequent Contributor
i wasn't suggesting that you ditch the geocoder widget entirely, just use the same approach demonstrated in the callback of another sample which zooms to a predefined scale level 'manually'.

that being said, you should be able to work with the second sample by mapping the value in the textbox to the actual parameter your own locator is expecting.

"Street": dom.byId("address").value
0 Kudos
TammyBearly
New Contributor
Our custom geocode service is doing the same thing. It keeps the extent of the map. How can you create a geocode service that would contain the extent?
0 Kudos
PLadd
by
Occasional Contributor III
I'm working on that now.  It's not finished but here's what I've got so far.

I place this line right after geocoder.startup();
geocoder.on("find-results", showResults);

And then I'm able to get the x, y, and extent from the following.  There's probably a cleaner way of doing this that a more skilled programmer might want to illustrate for us.

function showResults(results) {
         
          console.log(results.results.results[0].feature.geometry.x);
          console.log(results.results.results[0].feature.geometry.y);
          console.log(results.results.results[0].extent.xmax); //etc.
          console.log(results.results.results[0].extent.spatialReference.wkid);


The next step is to assign the x, y, and spatial reference to a point and zoom to it with this:
map.centerAndZoom(myPoint, 17);

I will post that when I'm done.
0 Kudos
PLadd
by
Occasional Contributor III
OK, this is fun! 

First, I didn't need to tear apart the results to get x, y, and spatial reference.  I simply need to use the geometry of the object itself as follows:
geom = results.results.results[0].feature.geometry;


and then use that as follows:
map.centerAndZoom(geom, 20);


Sadly, however, after all of that searching and coding, I learned that when I use the following for geocoding, I lose the ability to ID the map.
var gc = [{
              url: "http://MyURL/arcgis/rest/services/myGeocoder/GeocoderServer",
              name: "theGeocoder",
              singleLineFieldName: "Street"
          }];
          var geocoder = new esri.dijit.Geocoder({
              map: map,
              autoNavigate: true,
              autoComplete: true,
              geocoders: gc,
              geocoderMenu: false,
              arcgisGeocoder: false,
              placeholder: "142 East Main St"
          }, "search");
          geocoder.startup();


But with the generic code, I can ID the map
geocoder = new esri.dijit.Geocoder({
                  map: map,
                  autocomplete: true,
                  placehoder: "Find a place",
                  //searchExtent: search_extent,
                  arcgisGeocoder: {
                      name: "Esri World Geocoder"
                  }
              }, "search");
              geocoder.startup();


Anybody care to guess why I can't ID the map using this code?
0 Kudos
PLadd
by
Occasional Contributor III
You can all stop looking for an answer to my latest problem with ID'ing the map.  It was caused by a misplaced line of code resulting in this error:

Microsoft JScript runtime error: 'geocoder.inputNode' is null or not an object.

I simply moved geocoder.inputNode.blur(); to below map.centerAndZoom(geom, 20); and now I can ID the map again.

So, in the end, here's how I can zoom to the results of my customized geocode service.

var gc = [{
                      url: "http://MyServer/arcgis/rest/services/MyGeocoder/GeocodeServer",
                      name: "Geocoder",
                      singleLineFieldName: "Street"
                  }];
                  var geocoder = new esri.dijit.Geocoder({
                      map: map,
                      autoNavigate: false,
                      autoComplete: true,
                      geocoders: gc,
                      geocoderMenu: false,
                      arcgisGeocoder: false,
                      placeholder: "142 East Main St"
                  }, "search");
                  geocoder.startup();

                  geocoder.on("find-results", showResults);


function showResults(results) {
          var geom;

          for (var i = 0; i < results.results.results.length; i++) {
              console.debug(results.results.results, " at index ", i);
              geom = results.results.results[0].feature.geometry;
          }

          map.centerAndZoom(geom, 20);
          geocoder.inputNode.blur();
      }
0 Kudos