Adding points to graphicsLayer using feature's coordinates stored in attributes

921
2
Jump to solution
04-30-2013 07:58 AM
MikeOnzay
Occasional Contributor III
Is it possible to add graphic points to the map or graphicsLayer using coordinates stored in the feature? I'm using a query to collect the features I'm interested in. The function addToMap will display those polygons on a the map graphics layer or another graphics Layer (I did that just to test it). I want to add a point marker on top using the coordinates in the feature. The coordinate fields type are double. I haven't been able to retrieve those coordinates even to show them through console.log much less create a graphic point. Various attempts show undefined.

function addToMap(featureSet) {      //initialize symbology         defaultSymbol = new esri.symbol.SimpleMarkerSymbol().setColor(new dojo.Color([255,0,255]));            var iconSymbol = new esri.symbol.PictureMarkerSymbol({           "angle":0,           "xoffset":0,           "yoffset":10,           "type":"esriPMS",           "url":"http://static.arcgis.com/images/Symbols/Shapes/BluePin1LargeB.png",           "contentType":"image/png",           "width":24,           "height":24         });      var gLayer = new esri.layers.GraphicsLayer();         iTemplate = new esri.InfoTemplate("Sitename", "<tr><td>${Sitename}</tr></td>");         map.addLayer(gLayer);          dojo.forEach(featureSet.features,function(feature){           map.graphics.add(feature.setSymbol(defaultSymbol).setInfoTemplate(iTemplate));           //gLayer.add(feature.setSymbol(defaultSymbol).setInfoTemplate(iTemplate));           //var ptX = feature.attributes.centroidLongitude; // returns undefined           //var ptX = feature.centroidLongitude; // returns undefined           //var ptY = feature.attributes.centroidLatitude;    console.log(ptX);           gLayer.add(new esri.geometry.Point(ptX,ptY,map.spatialReference).setSymbol(iconSymbol));                      });
0 Kudos
1 Solution

Accepted Solutions
MikeOnzay
Occasional Contributor III
Finally figured it out. I needed to add

esri.geometry.geographicToWebMercator(new esri.geometry.Point
to this line
var pt = new esri.geometry.Point(ptX,ptY,map.spatialReference);
to make
var pt = new esri.geometry.Point(esri.geometry.geographicToWebMercator(new esri.geometry.Point(ptX,ptY,map.spatialReference)));


and it worked.

I still don't quite understand why I had to do this but I will leave that to a discussion post.

View solution in original post

0 Kudos
2 Replies
MikeOnzay
Occasional Contributor III
I figured out part of my problem. I did not capitalize my attribute so that it matches the field name - CentroidLongitude vs centroidLongitude. Now I can at least see the value in console.log.

Still can't figure out how to add graphic markers to a graphicsLayer using this information.
0 Kudos
MikeOnzay
Occasional Contributor III
Finally figured it out. I needed to add

esri.geometry.geographicToWebMercator(new esri.geometry.Point
to this line
var pt = new esri.geometry.Point(ptX,ptY,map.spatialReference);
to make
var pt = new esri.geometry.Point(esri.geometry.geographicToWebMercator(new esri.geometry.Point(ptX,ptY,map.spatialReference)));


and it worked.

I still don't quite understand why I had to do this but I will leave that to a discussion post.
0 Kudos