arcgis js:graphicLayer刪除特定的graphic

weixin_34321977發表於2018-08-29
9628812-91e55deed663e967.png
image.png

移除graphicLayer資料有兩個方法:clear()方法清空圖層資料、remove()方法傳入graphic引數移除特定的graphic。

9628812-66bae95904a4b7f0.png
軌跡勾選展示

上述勾選顯示軌跡的情況,就要對取消勾選時移除對應的軌跡。記錄該部分移除的程式碼如下

定義了全域性變數 ,用於儲存每次新增的線段

var tempObj = {};

新增與移除程式碼:

function selectClick() {
    $('#projectTable').on('click','.select',function () {
        var fileid = $(this).attr('data-id');
        if(!$(this).hasClass('cur')){
            $(this).addClass('cur');
            getRoadBinLocus(fileid);
        }else{
            $(this).removeClass('cur');
            polylineGraphicLayer.remove(tempObj[fileid]);
        }
    })
}

//獲取軌跡
function getRoadBinLocus(fileid) {
    //alert(fileid);
    //HXcommon.loader();
    var condition = {};
    // condition.freq=lastFreq;
    condition.fileName = fileid;
    $.post('/routeAnalysis/getRoadBinLocus', condition, function(json) {
        console.log(json);
        // $.post('/move/getRoadBinLocus',condition,function(json){
        var result = json.SUCCESS;
        HXcommon.loaderClose();
        if (result) {
            if (json.list.length == 0) {
                HXcommon.layer.alert("查無資料,請更換條件重新查詢!");
            } else {
                // 畫路軌
                drawLocus(json.list,fileid);
                console.log(json.list);
            }
        } else {
            HXcommon.layer.alert(json.MSG);
        }
    });
}

function drawLocus(list,fileid) {
    var lineArr = [];
    for (var k = 0; k < list.length; k++) {
        var lg = list[k].stationlg;
        var la = list[k].stationla;
        var gcj02Point = [];

        //對座標加偏移後疊加
        require(["hxdiGisModules/coordtransform"], function (coordtransform) {
            gcj02Point = coordtransform.wgs84togcj02(lg, la);
        });
        lineArr.push(gcj02Point);

    }
    //測試資料
    lineArr = [[120.0,30.13], [120.58,30.05],
        [120.07,30.08],[120.23,30.3]];
    createPolyLine(lineArr,fileid);
}

    function createPolyLine(lineArr,fileid) {
        require([

            "esri/geometry/Polyline", "esri/graphic",
            "esri/SpatialReference",
            "esri/symbols/SimpleLineSymbol", "esri/Color",
            "esri/layers/GraphicsLayer", "esri/geometry/Point",

            "dojo/domReady!" ], function(Polyline, Graphic,
                                         SpatialReference, SimpleLineSymbol, Color,
                                         GraphicsLayer, Point) {

            var polylineJson = {
                "paths" : [ lineArr ],
                "spatialReference" : {
                    "wkid" : 4326
                }
            };

            var polyline = new Polyline(polylineJson);

            var linesymbol = new SimpleLineSymbol(
                SimpleLineSymbol.STYLE_SOLID,
                new Color([0,255,0 ]),5);
            var linegraphic = new Graphic(polyline, linesymbol);
            polylineGraphicLayer.add(linegraphic)
            tempObj[fileid]=linegraphic;

        })

    }

相關文章