I am a beginner to ESRI javascript. I am attempting to write a script that will allow the user to enter a origin and destination address, display the points, and zoom to the extent of the two points. Using the search function I have been able to geocode both points and display them on the map. I would like to get the coordinates of each point and use them to set the extent of the map. Problem I am having is that I cannot access the coordinates of the points outside fromSearch.on('select-result', function(result). I set a global variable for the coordinates.
I can assign and get the coordinates within fromSearch.on('select-result', function(result), but if I try to access this variable outside this function I get Undefined. I cannot figure out what I am missing.
ready(function () {
parser.parse();
var extentInitial = new Extent({
"xmin" : -74.0249340000041,
"ymin" : 42.5025420000134,
"xmax" : -73.5927189999818,
"ymax" : 43.1172190002567,
"spatialReference" : {
"wkid" : 4326
}
});
mapMain = new Map("cpCenter", {
basemap : "osm",
extent : extentInitial,
});
var adaLayer = new FeatureLayer("http://services2.arcgis.com/A3lMfLhXIUtyxP6p/arcgis/rest/services/ADA_Aug14/FeatureServer/0",{
opacity : .5
});
mapMain.addLayer(adaLayer);
var symbol = new SimpleFillSymbol().setColor(new Color([255,0,0,0.5]));
var renderer = new SimpleRenderer(symbol);
adaLayer.setRenderer(renderer);
var scalebar = new Scalebar({
map: mapMain,
scalebarUnit: "dual"
});
var fromSearch = new Search({
enableLabel: true,
enableInfoWindow: false,
autoNavigate: false,
map: mapMain
}, "");
fromSearch.startup();
var toSearch = new Search({
enableLabel: true,
enableInfoWindow: false,
autoNavigate: false,
map: mapMain
}, "");
toSearch.startup();
dijit.registry.byId("fromButton").on("click", doSearchValue);
function doSearchValue(e) {
var fromAddress = document.getElementById("fromAddr").value
fromGeocodeSymbol = new SimpleMarkerSymbol(
SimpleMarkerSymbol.STYLE_CIRCLE,
20,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([85,82,82, 0.5]),
10
), new Color([76,230,0, 0.9]));
fromSearch.sources[0].highlightSymbol = fromGeocodeSymbol;
fromSearch.search(fromAddress);
fromSearch.on('select-result', function(result){
fromWebCoords = webMercatorUtils.webMercatorToGeographic(result.result.feature.geometry);
fromXCoord = fromWebCoords.x
console.log("inside local from function " + fromXCoord);
});
var toAddress = document.getElementById("toAddr").value
var toGeocodeSymbol = new SimpleMarkerSymbol(
SimpleMarkerSymbol.STYLE_CIRCLE,
20,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([85,82,82, 0.5]),
10
), new Color([245,29,29, 0.9]));
toSearch.sources[0].highlightSymbol = toGeocodeSymbol;
toSearch.search(toAddress);
toSearch.on('select-result', function(result){
toWebCoords = webMercatorUtils.webMercatorToGeographic(result.result.feature.geometry);
toXCoord = toWebCoords.x
console.log("inside local from function " + toXCoord);
});
console.log("Outside local function " + fromXCoord);
console.log("Outside local function " + toXCoord);
}
});