2013-10-12 55 views
1

我的工作,將來自各種源檢索數據,並從數據構造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)是有些多餘,但即使在地圖兩個位置的數據對象的點仍然沒有顯示在地圖上。

我已經工作了一段時間了,我真的很新鮮的想法。任何人可以提供幫助將不勝感激。謝謝。

+0

難道我的回答回答你的問題?如果是這樣,你應該接受它。 –

+0

沒有戴夫點仍然沒有繪製,但我感謝您的幫助。你爲我解決了一件事,所以我給了它一個投票。 –

回答

6

您的問題(或者至少是一個問題)是在你正試圖項目的地理座標的Web墨卡託行:

t = esri.geometry.geographicToWebMercator(
new esri.geometry.Point(attr["longitude"], attr["latitude"], gfx.spatialReference)); 

在點構造你正確地傳遞您的經度和緯度值,但是通過設置爲102100的gfx.spatialReference指定空間參考。102100是Web墨卡託的WKID。但是你的輸入數據是地理十進制度,所以你需要一個WKID = 4326的空間參考來表示GCS WGS84。所以你的行應看:

t = esri.geometry.geographicToWebMercator(
    new esri.geometry.Point(attr["longitude"], attr["latitude"], 
    new esri.SpatialReference(4326)); 

這是你的主要問題。

此外,線:

gfx.spatialReference = { wkid: 102100 }; 

沒有效果。 Graphics對象上沒有spatialReference屬性 - 僅在Graphics.geometry上 - 您正確設置它的位置。

只是最後的評論 - 你說你在這裏使用的,而不是要素圖層幾何層,因爲你需要能夠切換知名度,但你可以切換功能,包括任何圖層圖層的可見性。