I am having issues trying to set up a geocoder/locator on my server and also trying to query my geometry service when trying to project some lat/long coordinated. I keep getting an error SyntaxError {log: undefined, stack: (...), message: "Unexpected token <"}, but I can't seem to figure out why. I tried exploring the error message, but I don't get much good information out of it.
I have tried using ESRI's geocoder widget then created my own set up using the locator service. I have checked my address string, and I believe it should be passing correctly over to the server.
If I am understanding the error message right, it is saying there is a token "<" somewhere that shouldn't be there, but I cannot find it.
Other info... I am running version 9.3.1 for my server, using the JavaScript API 3.8 that ESRI hosts, and created an address locator on my server using street centerlines for the area I am mapping.
Has anyone run into this issue before or have suggestions for me? Right now, I am just trying to center the map on the location. Once I get that done, I can worry about all the other stuff.
My code for the locator is below. The site I am working on is at: Louisville Urban Tree Canopy Analysis.
/////////////////////////////////////////////////////////////////
locator = new Locator(singleFieldGeocodeURL);
//add handler for completion
locator.on("address-to-locations-complete", showResults);
//trying to find the error
locator.on("error", function (error) {
errorObject = error;
console.log("error" + error);
});
//add handler for button click
on(dom.byId('locate'), 'click', function () {
console.log(dom.byId('address').value);
map.graphics.clear();
var address = {
"SingleLine": dom.byId('address').value
};
locator.outSpatialReference = map.spatialReference;
var options = {
address: { street: address },
outFields: ["Loc_name"]
};
locator.addressToLocations(options);
});
function showResults(evt) {
console.log("in show results");
var candidate;
var symbol = new SimpleMarkerSymbol();
symbol.setStyle(SimpleMarkerSymbol.STYLE_SQUARE);
symbol.setColor(new Color([153, 0, 51, 0.75]));
var geom;
arrayUtils.event(evt.addresses, 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 Graphic(geom, symbol, attributes, null);
map.graphics.add(graphic);
return false;
}
});
if (geom !== undefined) {
map.centerAndZoom(geom, 12);
}
}
That's the type of error you get when trying to access a secure service without logging in/providing a token via your code. I would start by checking the security on your ArcGIS server rest service/folder. If you have security switched on, then you will need to provide a token (you should use a proxy page). If you don't need security just switch it off and try again.
Also, it would help if you posted a code snippet so we can see how you setup/use the Geocoder widget. Sometimes the token errors aren't related to security but have to do with some invalid parameter.
Here is the code I was using for the widget... I added in a div id='search' to try this on. I am not sure about the 3rd parameter of the myGeocoders list though. The first time I tried it, I wasn't using a 3rd parameter. I just tried it using the SingleField geocoder, and I got the same message.
var myGeocoders = [{ url: singleFieldGeocodeURL, name: "SingleField", singleLineFieldName: "ROADNAME" }, { url: alphaNumericGeocodeURL, name: "AlphaNumeric" }];
geocoder = new Geocoder({
map: map,
autoComplete: true,
arcgisGeocoder: false,
geocoders: myGeocoders
}, "search");
geocoder.startup();
Also, for sharing the the ArcGIS Server/Rest folder... I didn't set up the server so I am not sure if it is set properly. For the rest folder, do I have to grant the SOC and SOM user accounts read/write access to it?
Also, I have tried using the geometry service I set up, and I got the same error message. For that, I was trying to use HTML5's geolocation to position the map to the person's approximate location. I was trying to use the geometry service to reproject the lat/long coords into my map's projection. That code is:
on(map, "load", function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(ZoomToPosition);
}
});
function ZoomToPosition(position) {
var symbol = new SimpleMarkerSymbol();
symbol.setStyle(SimpleMarkerSymbol.STYLE_CIRCLE);
symbol.setColor(new Color([153, 0, 51, 0.75]));
var input = new Point(position.coords.latitude, position.coords.longitude, new SpatialReference({wkid: 4326}));
var PrjParams = new ProjectParameters();
PrjParams.geometries = [input];
PrjParams.outSR = new SpatialReference({ wkid: 2246 });
geometryService.project(PrjParams, function (outputPoint) {
var graph = new Graphic(outputPoint, symbol);
map.graphics.add(graph);
map.centerAndZoom(graph, 12);
})
}
Thanks again for any advice!
I found out from someone on GIS Stack Exchange that my issue was something as simple as a wrong URL. My URL wasn't point at the rest directory in my code.
That error is gone now. Thanks to anyone who was trying to help me out on this!