AnsweredAssumed Answered

Accessing coordinates of Search point outside results function in Javascript

Question asked by keng@cdta.org on Feb 2, 2017
Latest reply on Feb 3, 2017 by keng@cdta.org

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 () {
  // Parse DOM nodes decorated with the data-dojo-type attribute
  parser.parse();
  /*
  * Step: Specify the initial extent
  * Note: Exact coordinates may vary slightly from snippet/solution
  */

  var extentInitial = new Extent({
    "xmin" : -74.0249340000041,
    "ymin" : 42.5025420000134,
    "xmax" : -73.5927189999818,
    "ymax" : 43.1172190002567,
    "spatialReference" : {
      "wkid" : 4326
    }
  });
  // Create the map
  mapMain = new Map("cpCenter", {
    basemap : "osm",
    extent : extentInitial,
  });
  /*
  * Step: Add the ada layer layer to the map
  */

  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);
  /*
  * Step: Add the scalebar widget to the map
  */

  var scalebar = new Scalebar({
    map: mapMain,
    // "dual" displays both miles and kilmometers
    // "english" is the default, which displays miles
    // use "metric" for kilometers
    scalebarUnit: "dual"
  });
  //from address geocoder
  var fromSearch = new Search({
    enableLabel: true,
    enableInfoWindow: false,
    autoNavigate: false,
    map: mapMain
  }, "");
  fromSearch.startup();
  //to address geocoder
  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
    //alert(fromAddress);
    //highlight symbol
    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;
    //set the symbol for the highlighted symbol
    fromSearch.search(fromAddress);
    //get coordinates of toAddress to pass to use to create polyline for zooming to extent.
    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
    //alert(toAddress);
    //highlight symbol
    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;
    //set the symbol for the highlighted symbol
    //If multiple results are found, it will default and select the first.
    toSearch.search(toAddress);
    //get coordinates of toAddress to pass to use to create polyline for zooming to extent.
    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);
  }
});

Outcomes