Does anyone have code that demonstrates how to project all graphics in a graphics layer while maintaining their attributes?
Issue resolution to share after contacting tech support.
[The fundamental problem is that the project method on the Geometry service uses geometries and not graphics when projecting from one coordinate system to another. Another challenge that is associated with this problem is that the geometry service is async in nature meaning the sequential order of each geometry being projected may not be maintained. However this morning I spent some time taking a closer look at the project method and it take a list of geometries, and the order is maintained. I have attached the Url so you can take a look.
Conceptually approaching is issue you currently have an XML containing a list of items including geometries and attributes. Create two arrays; one for the geometry and one for the attributes. Perform the project using the geometry service and then conduct the loop.]
I implemented the following code. It reads data from an XML data source, creates source arrays for eometries and attributes and then calls the project method of the geometry service to add projected graphics with attributes to the graphics layer. Hope this helps someone.
********************************************
function mapCADXML()
{
var xmlhttp;
cadLayer.clear();
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var xmlEventList = this.responseXML;
var pt = new Point;
var symbol = PictureMarkerSymbol('images/CADCallLocation.png', 32, 32);
var sourcegeometries = [];
var sourceattributes = [];
//Loop through XML. Create geometry and attribute objects and store in separate arrays
for (idx = 0; idx < xmlEventList.getElementsByTagName("eventNbr").length; idx++)
{
var agency = (xmlEventList.getElementsByTagName("agency").item(idx).firstChild) ? xmlEventList.getElementsByTagName("agency").item(idx).firstChild.data : "";
if (agency == "WPK")
{
var eventNbr = (xmlEventList.getElementsByTagName("eventNbr").item(idx).firstChild) ? xmlEventList.getElementsByTagName("eventNbr").item(idx).firstChild.data : "";
var address = (xmlEventList.getElementsByTagName("address").item(idx).firstChild) ? xmlEventList.getElementsByTagName("address").item(idx).firstChild.data : "";
var longitude = (xmlEventList.getElementsByTagName("longitude").item(idx).firstChild) ? xmlEventList.getElementsByTagName("longitude").item(idx).firstChild.data : "";
var latitude = (xmlEventList.getElementsByTagName("latitude").item(idx).firstChild) ? xmlEventList.getElementsByTagName("latitude").item(idx).firstChild.data : "";
var eventtype = (xmlEventList.getElementsByTagName("eventtype").item(idx).firstChild) ? xmlEventList.getElementsByTagName("eventtype").item(idx).firstChild.data : "";
var eventdesc = (xmlEventList.getElementsByTagName("eventdesc").item(idx).firstChild) ? xmlEventList.getElementsByTagName("eventdesc").item(idx).firstChild.data : "";
var pointin = new Point(longitude, latitude, new SpatialReference({
wkid: 4326
}));
graphicattributes = {
"eventNbr": eventNbr,
"address": address,
"longitude": longitude
};
if (latitude > 0) {
graphic = new Graphic(pointin, symbol, graphicattributes);
sourceattributes.push(graphicattributes);
sourcegeometries.push(graphic.geometry);
}
}
}
//Call projection method of geometry service specifying newly created array of source geometries
gsvc.project(sourcegeometries, new SpatialReference({ wkid: 2236 }), function (projectedPoints)
{
var symbol = PictureMarkerSymbol('images/CADCallLocation.png', 32, 32);
//Create new projected graphics by looping through array of projected geometries
//and attach attributes based on common position in array.
for (idx = 0; idx < projectedPoints.length; idx++)
{
var geo = projectedPoints[idx];
var att = sourceattributes[idx];
var g = new Graphic(geo, symbol, att);
cadLayer.add(g);
}
});
}
}
xmlhttp.open("GET", [Put URL for XML service here], true);
xmlhttp.send();
}