我的工作,將來自各種源檢索數據,並從數據構造ESRI GraphicsLayer對象和在地圖上顯示它的應用程序。我之前創建了自定義的FeatureLayers,但該項目需要使用GraphicsLayers,因爲我需要能夠切換圖層的可見性。下面的代碼從主機獲取數據並將其放入GraphicsLayer中。自定義ArcGIS GraphicsLayer(JavaScript的)
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)
是有些多餘,但即使在地圖兩個位置的數據對象的點仍然沒有顯示在地圖上。
我已經工作了一段時間了,我真的很新鮮的想法。任何人可以提供幫助將不勝感激。謝謝。
難道我的回答回答你的問題?如果是這樣,你應該接受它。 –
沒有戴夫點仍然沒有繪製,但我感謝您的幫助。你爲我解決了一件事,所以我給了它一個投票。 –