Select to view content in your preferred language

Getting x and y coordinates of a graphic on a feature layer when clicking it

2478
7
Jump to solution
03-17-2014 05:53 AM
ChristianDebono
Emerging Contributor
I have a feature layer with a set of points in it. I want to get the x and y coordinates of a specific graphic when clicking it. I have done the following code but it is not giving the exact location.

on(map, "dbl-click", getCoordinates);  function getCoordinates(event) {                  var mp = esri.geometry.webMercatorToGeographic(event.mapPoint);                 alert(mp.x.toFixed(6) + ", " + mp.y.toFixed(6));                                                              }


[ATTACH=CONFIG]32230[/ATTACH]

I want to get the coordinates of the "M" symbol when the user clicks it.

Thanks for your help
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Try the following:

function selectInBuffer(response){   var feature;   var features = response.features;   var inBuffer = [];    for (var i = 0; i < features.length; i++) {     feature = features;     if(polygon.contains(feature.geometry)){       inBuffer.push(feature.attributes[featureLayer.objectIdField]);     }   }   var query = new Query();   query.objectIds = inBuffer;    featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(features, index){                     for(var i = 0; i < features.length; i++){           alert(features.geometry.x + ", " + features.geometry.y)       }   })           }

View solution in original post

0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Christian,

You can fire the 'getCoordinates' function when only the Feature Layer is clicked.  Ex:

var featureLayer = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0",{
    mode: FeatureLayer.MODE_ONDEMAND,
});
       
map.addLayer(featureLayer);

on(featureLayer, "click", getCoordinates);

function getCoordinates(event) {    
    var mp = esri.geometry.webMercatorToGeographic(event.mapPoint);
    alert(mp.x.toFixed(6) + ", " + mp.y.toFixed(6));                                       
}
0 Kudos
ChristianDebono
Emerging Contributor
Thanks a lot, worked perfectly
0 Kudos
ChristianDebono
Emerging Contributor
Is it possible to get the x and y coordinates of a specific point with out clicking it. For instance I written this code but it is not working.

featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function (features, index) {
                   
                    var xLat = "";
                    for (var x = 0; x < features.length; x++) {
                        xLat = features.geometry.extent.xmax;
                     
                    }
                });


Thanks for your help
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Yes, it's possible.  How are you selecting which point you want the coordinates for?  How do you want the coordinates returned (i.e. alert, dgrid)?
0 Kudos
ChristianDebono
Emerging Contributor
I'm selecting it by drawing a free hand polygon. A simple alert is enough. Below I have the code for selection

 function selectInBuffer(response) {
                var feature;
                var features = response.features;
                var inBuffer = [];

                for (var i = 0; i < features.length; i++) {
                    feature = features;
                    if (polygon.contains(feature.geometry)) {
                        inBuffer.push(feature.attributes[featureLayer.objectIdField]);
                    }
                }
                var query = new Query();
                query.objectIds = inBuffer;
           
                featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function (features, index) {
                    dom.byId('resultDiv').innerHTML = "Number of meters within polygon:  <b>" + features.length + "</b>";

                    var xLat= "";
                    for (var x = 0; x < features.length; x++) {
                        xLat = features.geometry.extent.xmax;
                    }

                });
            }
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try the following:

function selectInBuffer(response){   var feature;   var features = response.features;   var inBuffer = [];    for (var i = 0; i < features.length; i++) {     feature = features;     if(polygon.contains(feature.geometry)){       inBuffer.push(feature.attributes[featureLayer.objectIdField]);     }   }   var query = new Query();   query.objectIds = inBuffer;    featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(features, index){                     for(var i = 0; i < features.length; i++){           alert(features.geometry.x + ", " + features.geometry.y)       }   })           }
0 Kudos
ChristianDebono
Emerging Contributor
Thanks for your help, worked perfectly!
0 Kudos