6 Replies Latest reply on Aug 28, 2013 5:02 AM by ZachLiu

    Mark Point to get Coordinates, then identify.

    DK7686
      Hi

      I'm using a MarkPoint function which gives me some coords in a pop up. I'm trying to send them to an identify function.

      The identify function would normally work 'onClick' as per below.

      function mapReady(map){
             dojo.connect(map,"onClick",executeIdentifyTask); 

      My alerts show that the script runs through the mapReady function, then does nothing once it reaches the fucntion executeIdentifyTask itself.

      It's not being fed the identify parameters (normally this would be evt.mapPoint). Does anyone know how I pass my original MarkPoint throught to my identify. What I'm after if for the end user to gen get attributes and coordinates fro a click on the map.

      function executeIdentifyTask(evt) {
              identifyParams.geometry = evt.mapPoint;


      Using version 3.6 by the way - the script is below if anyone can spot the clanger I'd love to know.

      Cheers
      Dave

      :::::::::::::::::::::::::::::::::::::::::::::::::::::

        <script>
            dojo.require("esri.map");
            dojo.require("esri.tasks.geometry");
         dojo.require("esri.dijit.Popup");

            var map = null;
            var gsvc = null;
            var pt = null;
         var identifyTask, identifyParams;

            function initialize() {
        
             var popup = new esri.dijit.Popup({
                fillSymbol: new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 2), new dojo.Color([255,255,0,0.25]))
              }, dojo.create("div"));
       
              map = new esri.Map("map");

      var background = new esri.layers.ArcGISTiledMapServiceLayer("http://bl-arcgis-1/ArcGIS/rest/services/Basemaps/MapServer");
              map.addLayer(background);
              map.setExtent(new esri.geometry.Extent(450000, 500000, 480000, 530000, new esri.SpatialReference({wkid: 27700}))); 
       

              gsvc = new esri.tasks.GeometryService("http://bl-arcgis-1/ArcGIS/rest/services/Geometry/GeometryServer");
              dojo.connect(map, "onClick", markpoint);
        //dojo.connect(map, "onLoad", mapReady);
            };

            function markpoint(evt) {
        
              map.graphics.clear();
             
              var point = evt.mapPoint;
              var symbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
              var graphic = new esri.Graphic(point, symbol);
              var outSR = new esri.SpatialReference({ wkid: 27700});
             
              map.graphics.add(graphic);
              gsvc.project([ point ], outSR, function(projectedPoints) {
                pt = projectedPoints[0];
                graphic.setInfoTemplate(new esri.InfoTemplate("HERE?",
                  "<p> X: " + pt.x +
                  "<br/> Y: " + pt.y +
                  "</p>"  +
                  "<input type='button' value='CONFIRM' onclick='mapReady(map);' />" +
                  "<div id='latlong'></div>"));
                map.infoWindow
                  .setTitle(graphic.getTitle())
                  .setContent(graphic.getContent())
                  .show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
              });
          }


      function mapReady(map){
      //function executeIdentifyTask(){
      alert("line 87");
             dojo.connect(map,"onLoad",executeIdentifyTask);
             //create identify tasks and setup parameters 
             identifyTask = new esri.tasks.IdentifyTask("http://bl-arcgis-1/ArcGIS/rest/services/CRM/MapServer");
             
             identifyParams = new esri.tasks.IdentifyParameters();
          identifyParams.tolerance = 3;
             identifyParams.returnGeometry = false;
             identifyParams.layerIds = [1,2,3,4];
             identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
             identifyParams.width  = map.width;
             identifyParams.height = map.height;
           alert("Line 98") }
          
            function executeIdentifyTask(evt) {
              identifyParams.geometry = evt.mapPoint;
          // identifyParams.geometry = evt.screenPoint;
              identifyParams.mapExtent = map.extent;
             
              var deferred = identifyTask.execute(identifyParams);

              deferred.addCallback(function(response) {     
                // response is an array of identify result objects    
                // Let's return an array of features.
                return dojo.map(response, function(result) {
                  var feature = result.feature;
                  feature.attributes.layerName = result.layerName;
                  if(result.layerName === 'Adopted Highways'){
                   // console.log(feature.attributes.PMH);
                    var template = new esri.InfoTemplate("DKDKDKDKDK","PMH:${PMH}");
                    feature.setInfoTemplate(template);
                  }
                  else if (result.layerName === 'Council Owned'){
                    var template = new esri.InfoTemplate("BCO","BCO:${BCO}");
                    feature.setInfoTemplate(template);
                  }
         else if (result.layerName === 'LSVT'){
                    var template = new esri.InfoTemplate("LSVT","LSVT:${LSVT}");
                    feature.setInfoTemplate(template);
                  }
         else if (result.layerName === 'Ward'){
                    var template = new esri.InfoTemplate("NAME","NAME:${NAME}");
                    feature.setInfoTemplate(template);
                  }
                  return feature;
                });
              });

            
              // InfoWindow expects an array of features from each deferred
              // object that you pass. If the response from the task execution 
              // above is not an array of features, then you need to add a callback
              // like the one above to post-process the response and return an
              // array of features.
              map.infoWindow.setFeatures([ deferred ]);
              map.infoWindow.show(evt.mapPoint);
            }
            dojo.ready(initialize);
          </script>
        • Re: Mark Point to get Coordinates, then identify.
          kenbuja
          I think the issue is that you're putting the executeIdentifyTask into the map's onLoad event, so it's not going to fire when you expect. Instead, you want this to fire when the user clicks on the Confirm button. Try something like this instead

          gsvc = new esri.tasks.GeometryService("http://bl-arcgis-1/ArcGIS/rest/services/Geometry/GeometryServer");
                  dojo.connect(map, "onClick", markpoint);
            dojo.connect(map, "onLoad", mapReady); 
          
          function markpoint(evt) {
          
              //rest of function
          
              graphic.setInfoTemplate(new esri.InfoTemplate("HERE?",
                  "<p> X: " + pt.x +
                              "<br/> Y: " + pt.y +
                              "</p>"  +
                              "<input type='button' value='CONFIRM' onclick='executeIdentifyTask(evt);' />" +
                              "<div id='latlong'></div>"));
          
              //rest of function
          }
          
          function mapReady(map){ 
              //function executeIdentifyTask(){
              alert("line 87");
                     //dojo.connect(map,"onLoad",executeIdentifyTask); 
                     //create identify tasks and setup parameters  
                     identifyTask = new esri.tasks.IdentifyTask("http://bl-arcgis-1/ArcGIS/rest/services/CRM/MapServer"); 
          
                      // rest of function
          } 
          
          


          Please remember to put your code into the [CODE] block (using the # button above). It makes it much easier to read.
          • Re: Mark Point to get Coordinates, then identify.
            DK7686
            Thanks for the response... still stuck I'm afraid. The error I'm getting is that: evt is undefined.

            Normally the identify runs from a user click, defined by the line in red below.... so I'm missing something.
            I've highlighted in green where the script get to...

            How do I define this evt. From the original markpoint(evt)?

            Cheers in advance, any help greatly appreciated.

            gsvc = new esri.tasks.GeometryService("http://bl-arcgis-1/ArcGIS/rest/services/Geometry/GeometryServer");
            dojo.connect(map, "onClick", markpoint);
            dojo.connect(map, "onLoad", mapReady);

            };

            function markpoint(evt) {

            map.graphics.clear();

            var point = evt.mapPoint;
            var symbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
            var graphic = new esri.Graphic(point, symbol);
            var outSR = new esri.SpatialReference({ wkid: 27700});

            map.graphics.add(graphic);
            gsvc.project([ point ], outSR, function(projectedPoints) {
            pt = projectedPoints[0];
            graphic.setInfoTemplate(new esri.InfoTemplate("HERE?",
            "<p> X: " + pt.x +
            "<br/> Y: " + pt.y +
            "</p>" +
            "<input type='button' value='CONFIRM' onClick='executeIdentifyTask(evt);' />" +
            "<div id='latlong'></div>"));
            map.infoWindow
            .setTitle(graphic.getTitle())
            .setContent(graphic.getContent())
            .show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
            });
            }


            function mapReady(map){
            //create identify tasks and setup parameters
            //dojo.connect(map,"onClick",executeIdentifyTask);
            identifyTask = new esri.tasks.IdentifyTask("http://bl-arcgis-1/ArcGIS/rest/services/CRM/MapServer");
            identifyParams = new esri.tasks.IdentifyParameters();
            identifyParams.tolerance = 3;
            identifyParams.returnGeometry = false;
            identifyParams.layerIds = [1,2,3,4];
            identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_ALL;
            identifyParams.width = map.width;
            identifyParams.height = map.height;
            }

            function executeIdentifyTask(evt) {
            identifyParams.geometry = evt.mapPoint;
            identifyParams.mapExtent = map.extent;
            • Re: Mark Point to get Coordinates, then identify.
              ZachLiu
              When you click the button the event object is associate the button so execute identifyTask function there will not work. You may save the evt object from the map click event and pass it to your identifyTask function,

              or I think you can just populate all information into a hidden infoWindow when users click on the map. And when users click the button later on, you can just execute infoWindow.show().:)
              • Re: Mark Point to get Coordinates, then identify.
                zj_zou
                1. Zach is right. Make a copy of evt object passed to markpoint, and feed it to executeIdentifyTask.
                2. Is the project task completed successfully? I noticed that since v3.6 (and you are using v3.6), the schema of project task is changed. Please refer to the ESRI document, https://developers.arcgis.com/en/javascript/jsapi/geometryservice.html#project.

                Try the revised markpoint function, assuming the project task works.
                function markpoint(evt) {
                  map.graphics.clear();
                
                  var evtCopy = evt;
                  var point = evt.mapPoint;
                  var symbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
                  var graphic = new esri.Graphic(point, symbol);
                  var outSR = new esri.SpatialReference({ wkid: 27700});
                
                  map.graphics.add(graphic);
                  gsvc.project([ point ], outSR, function(projectedPoints) {
                    pt = projectedPoints[0];
                    graphic.setInfoTemplate(new esri.InfoTemplate("HERE?",
                      "<p> X: " + pt.x +
                      "<br /> Y: " + pt.y +
                      "</p>" +
                      "<input type='button' value='CONFIRM' onclick='executeIdentifyTask(evtCopy);' />" +
                      "<div id='latlong'></div>"));
                    map.infoWindow
                      .setFeatures([graphic])
                      .show(evtCopy.screenPoint, map.getInfoWindowAnchor(evtCopy.screenPoint));
                  });
                }
                • Re: Mark Point to get Coordinates, then identify.
                  DK7686
                  Thanks for the response - much appreciated.

                  We never did get to the bottom on why the evt wouldn't pass the markpoint, so we worked around this. What we were after is all the results at the same time so we swapped the order they ran and binned off running a function from the button.

                  We run the Identify function first, then run the Markpoint. Had to be careful with how we ran the markpoint though, it will run before the identify results are returned unless you're careful.... leaving us with out the identify data. So we used the following to runt eh markpoint under the identify function.

                    }).then(function(){markpoint(evt)});

                  We're sending all the results to a table under them map so we didn't need to worry about the pop ups, and send the values in a URL.

                  Got there in the end.

                  Cheers
                  Dave
                  • Re: Mark Point to get Coordinates, then identify.
                    ZachLiu
                    David, the problem is for this line
                    "<input type='button' value='CONFIRM' onClick='executeIdentifyTask(evt);' />"


                    you bind your executeIdentifyTask function to the ("button", "click") event, it's similar to this,

                    dojo.connect('button', 'onClick', executeIdentifyTask)


                    so the event passed to your handler is a button click event not a map click event. Because a button and a map are two different objects, there's no event.mapPoint property with the event object. It is a little tricky here.

                    But in your markpoint function which is triggered by map click event, if you add the line added by Jason,

                     var evtCopy = evt;


                    you assign the map click event object to store it in a variable in this way, and then when you trigger button click event, you can manually pass the stored map click event object to executeIdentifyTask function.

                    Hope this will help.