折线几何到图形 - SimpleLineSymbol 是唯一不显示在地图上的类型

Polyline geometry to graphic - SimpleLineSymbol is the only type not displaying on the map

我修改了 ESRI ArcGIS js API 测量小部件以保留基于会话的用户测量历史记录。当用户单击历史项时,它应该将与该历史项关联的几何图形显示为地图上的 GraphicsLayer。我正在使用 knockout 来管理历史项并在单击历史项时检索测量元数据。

此时,我的多边形(用于区域)和点(用于位置)分别与 SimpleFillSymbol() 和 SimpleMarkerSymbol() 一起工作得很好。但是,使用 SimpleLineSymbol() 时,从距离测量返回的折线几何图形不会显示在地图上。

代码如下:

var graphicLayerId = "measurementHistoryGraphicsLayer";

function addGraphicsLayerToMap(graphicsLayer) {
    var lay = getGraphicsLayerFromMap();
    if (lay !== undefined) {
        lay.clear();
        lay.add(graphicsLayer);
        map.removeLayer(lay);
    }
    map.addLayer(graphicsLayer);
}


function createGraphicFromGeometry(viewModel) {
    //Determine the symbol type
    var symbol;
    switch (viewModel.activeTool) {
        case "area":
            symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,
                                                      new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new esri.Color([255, 0, 0]), 2),
                                                      new esri.Color([255, 0, 0, 0.25]));
            break;
        case "distance":
            symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new esri.Color([255, 0, 0]), 3);
            break;
        case "location":
            symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10,
                                                            new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new esri.Color([255, 0, 0]), 3),
                                                            new esri.Color([0, 255, 0, 0.25]));
            break;
    }
    var graphic = new esri.Graphic(viewModel.geometry, symbol, { "extent": viewModel.extent, "unitName": viewModel.unitName });
    return graphic;
}

function createGraphicsLayerFromGraphic(graphic) {
    var graphicLayer = new esri.layers.GraphicsLayer({ id: graphicLayerId });
    graphicLayer.add(graphic);
    graphicLayer.setRenderer(new esri.renderer.SimpleRenderer(graphic.symbol));
    return graphicLayer;
}

function getGraphicsLayerFromMap() {
    return map.getLayer(graphicLayerId);
}

$(document).on('click', '#emv_measurement_history .list-group-item', function () {
    $('#emv_measurement_history .list-group-item.list-group-item-info').removeClass('list-group-item-info');
    $(this).addClass('list-group-item-info');
    var measurementData = ko.mapping.toJS(ko.dataFor($(this)[0]));
    var graphic = createGraphicFromGeometry(measurementData);
    var graphicsLayer = createGraphicsLayerFromGraphic(graphic);
    addGraphicsLayerToMap(graphicsLayer);
    map.setExtent(measurementData.extent);
});

正如我所说,这对区域和位置都适用,但距离似乎不起作用。我什至尝试在其中添加一个硬编码的多段线值,但没有成功地从中创建一个 SimpleLineSymbol。

有关更多信息,这里是折线信息:

[
    [
        [
            2591769.2297164765,
            5236836.417134136
        ],
        [
            2573584.2281166334,
            4620357.96034264
        ],
        [
            2557384.1428811993,
            4038303.8136230526
        ],
        [
            3124973.8484519687,
            4260007.60486125
        ],
        [
            3714518.451309448,
            4454862.77067183
        ],
        [
            4324318.833989203,
            4618552.510359674
        ],
        [
            4666465.839330839,
            4693607.843734423
        ],
        [
            5013294.285789721,
            4757423.375729576
        ]
    ]
]

并且空间参考设置为 102100。

我终于明白了。

我将原始测量的几何形状存储在一个剔除变量中。当我从中读取时,它会很好地构建图形、符号和图形层,不会抛出任何错误。

我发现由于某种原因,数据和空间参考不匹配,所以我从存储的几何图形中提取路径,将其分配给一个新的多段线变量,根据需要将空间参考重新设置为 102100 , 并将几何图形重新分配给图形,效果很好。

var g = new esri.Graphic(viewModel.geometry, symbol, { "extent": viewModel.extent, "unitName": viewModel.unitName });
if (viewModel.activeTool === "distance") {
    var polyline = new esri.geometry.Polyline(viewModel.geometry.paths);
    polyline.setSpatialReference(new esri.SpatialReference(102100));
    g.setGeometry(polyline);
}