2014-03-27 28 views
2

解決了......我想獲得分組圖形的幾何圖形,我似乎無法獲得x和y而無需將graphic.attributes.baseGraphic添加到新圖形I必須創造。有問題的代碼具有定義問題部分的「***」字符。ESRI Javascript API獲取程序的功能

handleMouseOver: function(evt) { 
     var graphic = evt.graphic; 
     var latlon = evt.graphic.geometry; 
     //alert('x =' + latlon.x + 'y =' + latlon.y); 
     if (graphic.symbol.type == 'textsymbol' || graphic.symbol.type == 'simplelinesymbol') { 
      if (graphic.attributes) { 
       if (graphic.attributes.baseGraphic && graphic.attributes.baseGraphic.task) { 
        graphic.attributes.baseGraphic.task.cancel(); 
       } 
      } 
      //return; 
     } 
     if (graphic.attributes.isCluster) { //cluster mouse over 
      if (graphic.attributes.clustered) { 
       for (var i = 0; i < graphic.attributes.clusterSize; i++) 
       { 
        x = graphic.attributes[i].baseGraphic.geometry.x; 

        y = graphic.attributes[i].baseGraphic.geometry.y; 
        //alert('x=' + x + 'y=' + y); 
       } 
       if (graphic.task) { 
        graphic.task.cancel(); 
       } 
       //return; 
      } 
     } else { //single marker or cluster flare mouse over 
      if (graphic.attributes.baseGraphic) { //cluster flare 
       graphic.attributes.baseGraphic.task.cancel(); 
      } 
      this.showInfoWindow(graphic); 
      //return; 
     } 

     graphic.clusterGraphics = []; 
     //alert('x =' + latlon.x + 'y =' + latlon.y); 
     var cSize = graphic.attributes.clusterSize; 
     var lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0, 1]), 1); 

     //polyline used to "tie" flare to cluster 
     //set up initially with the center pt of the cluster as the first point and a dummy point @ 0,0 for a placeholder 
     var line = new esri.geometry.Polyline(map.spatialReference); 
     line.addPath([graphic.geometry, new esri.geometry.Point(0, 0)]); 

     //polyline graphic 
     var lineGraphic = new esri.Graphic(line, lineSymbol); 

     //creating a circle to evenly distribute our flare graphics around 
     if (cSize > 1 && cSize <= this._flareLimit) { //cSize > 1 may not be needed 
      //takes the number of points (flares) for the cluster 
      var numPoints = graphic.attributes.clusterSize; 

      //takes the pixel distance from the center of the graphic to flare out the graphics 
      var bufferDistance = this.getPixelDistanceFromCenter(graphic.geometry); 

      //center of cluster graphic 
      var centerPoint = graphic.geometry; 


      //variables used to plot points evenly around the cluster 
      var dblSinus, dblCosinus, x, y, pt, ptGraphic, p, l; 


      ***for (var i = 0; i < graphic.attributes.clusterSize; i++) { 
        //constructing the flare graphic point 


       //pt = new esri.geometry.Point(x, y, this._map.spatialReference) 
       //ptGraphic = new esri.Graphic(pt, this.symbolBank.single, dojo.mixin(graphic.attributes[i], { baseGraphic: graphic }), this._infoTemplate); 
       x = graphic.attributes[i].baseGraphic.geometry.x; 

       y = graphic.attributes[i].baseGraphic.geometry.y;*** 

       //constructing the flare graphic point 
       //pt = new esri.geometry.Point(x, y, this._map.spatialReference) 
       //ptGraphic = new esri.Graphic(pt, this.symbolBank.single, dojo.mixin(graphic.attributes[i], { baseGraphic: graphic }), this._infoTemplate); 
       //alert('pt.x=' + pt.x + 'pt.y=' + pt.y) 
       //try to always bring flare graphic to front of everything else 
       p = this.add(ptGraphic); 
       //p.getDojoShape().moveToFront(); 

       //reset our 0,0 placeholder point in line to the actual point of the recently created flare graphic 
       line.setPoint(0, 1, pt); 
       lineGraphic = new esri.Graphic(line, lineSymbol, { baseGraphic: graphic }); 

       //try to always have connector line behind everything else 
       l = this.add(lineGraphic); 
       //l.getDojoShape().moveToBack(); 

       //store flare graphic and connector graphic 
       graphic.clusterGraphics.push(p); 
       graphic.clusterGraphics.push(l); 
      } 

      //set "clustered" flag 
      graphic.attributes.clustered = true; 
     } 
    }, 
+0

...你真正的問題是什麼?在您說有問題的部分中,您有很多註釋掉的代碼,這使我們很難確定哪些內容無效或無效。 – Juffy

回答

0

根據您的「有限的問題」上面的這部分:

「我試圖讓分組圖形的幾何形狀」

我假設你正在使用esri ClusterLayer如下所述:https://developers.arcgis.com/javascript/jssamples/layers_point_clustering.html

如果是這樣,那麼在這裏我們去:

鼠標移到不會在所有的原因是羣集的圖像僅僅是它們在不同的破發點,例如設置針對新esri.renderer.ClassBreaksRenderer PictureMarkerSymbols理想:

var renderer = new esri.renderer.ClassBreaksRenderer(mapManager.pointFeatureSymbol, "clusterCount"); 
var blue = new esri.symbol.PictureMarkerSymbol("images/BluePin1LargeB.png", 32, 32).setOffset(0, 15); 
var green = new esri.symbol.PictureMarkerSymbol("images/GreenPin1LargeB.png", 64, 64).setOffset(0, 15); 
var red = new esri.symbol.PictureMarkerSymbol("images/RedPin1LargeB.png", 72, 72).setOffset(0, 15); 

    renderer.addBreak(0, 2, blue); 
    renderer.addBreak(2, 200, green); 
    renderer.addBreak(200, 1001, red); 

您可以點擊圖像相當,並獲得圖形內做類似如下:

現在,假設你有一個簇層對象的地方:讓說VAR clusterLayer =新ClusterLayer

您可以附加一個事件

dojo.connect(map, "onClick", function (evt) 
{ 
     var mp = evt.mapPoint; 
     var cl= map.getLayer("Points"); 

     for (var g in cl.graphics) 
     { 
      var graphic = cl.graphics[g]; 
      if (graphic.geometry != null && graphic.geometry.type == "point") 
      { 
       var toleranceInPixel = 10; 

       // now create a buffer around the current graphic and basically build    a polygon for it and get its extent 
       var extentPoly = pointToPolygon(graphic.geometry, toleranceInPixel); 

       // then see if this buffered point polygon contans the mp 
       if (extentPoly .contains(mp)) 
       { 
        // add all points that are within your buffered polygon to some array and access their geometries from there 
       } 
      } 
     } 
}); 

方法來構建從點的一多邊形 - 注具有這種構建在

function pointToPolygon (point, toleranceInPixel) 
    { 
    //calculate map coords represented per pixel 
    var pixelWidth = mapManager.map.extent.getWidth()/mapManager.map.width; 

    //calculate map coords for tolerance in pixel 
    var toleraceInMapCoords = toleranceInPixel * pixelWidth; 

    //calculate & return computed extent 
    var extent = esri.geometry.Extent(point.x - toleraceInMapCoords, 
        point.y - toleraceInMapCoords, 
        point.x + toleraceInMapCoords, 
        point.y + toleraceInMapCoords, 
        map.spatialReference); 


    return (convertExtentToPolygon(extent, map.spatialReference)); 
} 

方法建立從多邊形的程度,可以更新ESRI庫 - 注有可被更新ESRI庫,其具有此版本在

function convertExtentToPolygon (extent, spatialRef) 
    { 
    var xmin = extent.xmin; 
    var xmax = extent.xmax; 
    var ymin = extent.ymin; 
    var ymax = extent.ymax; 

    var topLeft = new esri.geometry.Point(xmin, ymax, spatialRef); 
    var topRight = new esri.geometry.Point(xmax, ymax, spatialRef); 
    var bottomRight = new esri.geometry.Point(xmax, ymin, spatialRef); 
    var bottomLeft = new esri.geometry.Point(xmin, ymin, spatialRef); 

    var rings = new Array(topLeft, topRight, bottomRight, bottomLeft, topLeft); 

    var newPolygon = new esri.geometry.Polygon(spatialRef); 
    newPolygon.addRing(rings); 
    return newPolygon; 
} 

您可以根據需要調整公差 - 有可能是新的ESRI庫做得更好,但這是類似於前一段時間使用。

+0

謝謝。這太棒了。 – user1584981

+0

請標記我的答案,如果它對你有幫助。謝謝。 –

+0

我無法將其標記爲已解決。奇怪,我想我沒有選擇。 – user1584981