我正在开发一个应用程序,该应用程序将从各种来源检索数据并从数据中构造 ESRI GraphicsLayer 对象并将其显示在地图上。我之前创建了自定义 FeatureLayers,但这个项目需要使用 GraphicsLayers,因为我需要能够切换图层的可见性。下面的代码从主机获取数据并将其放入 GraphicsLayer。
define(["dojo/_base/declare", "dojo/_base/array", "dojo/request", "esri/graphic", "esri/geometry/Geometry", "esri/InfoTemplate"],
function(declare, array, request, Graphic, Geometry, InfoTemplate) {
return declare(null, {
getAllCurrentReadings: function() {
var rtn = [];
var stations = ["S", "SN", "AN", "UP", "GR", "PL", "SR", "J", "N", "FL"];
array.forEach(stations, function(item, i) {
request.post("includes/buoybay_proxy.php", {
data: {
"method": "RetrieveCurrentReadings",
"params": "CBIBS," + item + ",113f8b...f27e0a0bb" // NOTE: id: 1 is necessary as well but is added manually by jsonRPCClient
},
sync: true,
handleAs: "json"
}).then(
function(response) {
var gfx, attr, t;
//console.log(response);
// Now build the Graphic Object and push it into rtn
gfx = new Graphic();
gfx.spatialReference = {
wkid: 102100
};
// Define attribute object
attr = {};
attr["station"] = response.station;
attr["title"] = translateStationID(response.station);
for (var j = 0; j < response.measurement.length; j++) {
attr[String(response.measurement[j])] = response.value[j];
}
gfx.attributes = attr;
// Define geometry object
gfx.geometry = new Geometry(gfx.spatialReference, "point");
gfx.geometry.spatialReference = {
wkid: 102100
};
gfx.geometry.type = "point";
t = esri.geometry.geographicToWebMercator(new esri.geometry.Point(attr["longitude"], attr["latitude"], gfx.spatialReference));
gfx.geometry.x = t.x;
gfx.geometry.y = t.y;
// Define infoTemplate object
gfx.infoTemplate = new esri.InfoTemplate();
gfx.infoTemplate.setTitle(attr["title"]);
gfx.infoTemplate.setContent("${*}");
// Define symbol
gfx.symbol = new esri.symbol.PictureMarkerSymbol("../images/marker.png", 15, 15);
//console.log(gfx);
rtn.push(gfx);
},
function(error) {
console.log("Error: " + error + "\n");
}
)
});
//console.log(rtn);
return rtn;
}
})
})
这段代码似乎正确地构造了 GraphicsLayers,但是当我将它们添加到地图对象时,地图上没有显示任何点。我用来将它们添加到地图对象的代码如下。
require(["dojo/parser", "dojo/_base/array", "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/ready", "esri/map", "esri/layers/ArcGISTiledMapServiceLayer", "js/cbibsGfxModule", "dojo/domReady!"],
function(parser, array, BorderContainer, ContentPane, ready, map, ArcGISTiledMapServiceLayer, cbibsGfxModule) {
var Map, cbibs, gfxLayer, t = [];
function init() {
Map = new map("mapDiv", {
basemap: "oceans",
center: [-77.0357, 38.7877],
zoom: 7
});
dojo.connect(Map, "onLoad", displayData); // Map didn't load until 3rd arg was a function name; why?
function displayData() {
cbibs = new cbibsGfxModule();
t = cbibs.getAllCurrentReadings();
gfxLayer = new esri.layers.GraphicsLayer();
array.forEach(t, function(item) {
gfxLayer.add(item);
Map.graphics.add(item);
});
gfxLayer.spatialReference = {
wkid: 102100
};
//Map.addLayer(gfxLayer); // Add GraphicsLayer to Map object
console.log(Map); // Custom GraphicLayers are under _layers
};
};
dojo.ready(init);
}
);
我意识到这有点多余gfxLayer.add(item)
,Map.graphics.add(item)
但即使在 Map 对象中的两个位置有数据,这些点仍然没有显示在地图上。
我已经为此工作了一段时间,我真的很新鲜。任何人都可以提供的任何帮助将不胜感激。谢谢你。