ArcGIS JS之 applyEdits之后要素符號更新


ArcGIS JS版本 ArcGIS JS 4.11

最近做一個地圖服務,通過FeatureLayer.applyEdits()方法,更新唯一值的渲染字段,實現地圖渲染根據用戶的配置實時更新。

由於ArcGIS JS自帶的編輯Widget效果不好,自己做了一個更新彈窗,applyEdits之后,發現該地圖符號不會自動更新,需要地圖視圖稍微變化一點,才會刷新成最新的渲染

查詢了官網API,發現ArcGIS JS無對FeatureLayer中某個Feature的渲染進行刷新的方法,無奈只能調用FeatureLayer.refresh()方法刷新整個圖層。

可執行完之后,發現並沒有實現地圖渲染的刷新。

最初的核心代碼

添加矢量圖層代碼

            var layerW = new MapImageLayer({
                url: mapServerUrl,
                opacity: 0.7
            });
            map.add(layerW); // adds the layer to the map            

彈出更新Form代碼 注:其中showModal為一個彈出bootstrap Modal對象的方法

此處的FeatureLayer是通過具體的圖層名稱,去重新實例化的對象

var popup = view.popup;
                // var editFeature=null;
                popup.viewModel.on("trigger-action", function(event) {
                    var ftlyr=null;
                    // editFeature=popup.viewModel.selectedFeature;

                    var lyc = getLyrConfigByLyrId(event.action.id);
                    ftlyr= new FeatureLayer({
                        url:FeatureServerUrl+lyc.id
                    });

                    var oid = popup.viewModel.selectedFeature.attributes["OBJECTID"];
                    editFeature=selectFeature(ftlyr,oid);
                    // 根據Guid 和action id彈出相關編輯界面,並提交流程狀態/備份等
                    var attributes = popup.viewModel.selectedFeature.attributes;
                    if(lyc.id==1){
                        //一事一辦
                        var data={};
                        data.State=attributes["完成情況"];
                        data.Summary = attributes["備注"];
                        showModal({
                            ModalName:"Ysyb",
                            Mode:"edit",
                            PstUrl:"#",
                            Data:data,
                            Title:"一事一辦項目上報",
                            submitHandler:function () {
                                editFeature.attributes["State"]=$("#YsybForm select[name='State']").val()
                                editFeature.attributes["Summary"]=$("#YsybForm textarea[name='Summary']").val();
                                const edits = {
                                    updateFeatures: [editFeature]
                                };

                                applyAttributeUpdates(ftlyr,edits);
                            }
                        });
                    }

屬性更新代碼

function applyAttributeUpdates(ftlyr,params) {
                dom.byId("viewDiv").style.cursor = "progress";
                ftlyr.applyEdits(params).then(function(editsResult) {
                    // Get the objectId of the newly added feature.
                    // Call selectFeature function to highlight the new feature.
                    if (editsResult.addFeatureResults.length > 0) {
                        const objectId = editsResult.addFeatureResults[0].objectId;
                        selectFeature(ftlyr,objectId);
                    }
                    if(editsResult.updateFeatureResults.length > 0){
                        unselectFeature();
                        const objectId = editsResult.updateFeatureResults[0].objectId;
                        ftlyr.refresh();
                        $.notify({message:"更新成功"},{type:"success"});
                    }
                    dom.byId("viewDiv").style.cursor= "auto";
                })
                    .catch(function(error) {
                        console.log("===============================================");
                        console.error(
                            "[ applyEdits ] FAILURE: ",
                            error.code,
                            error.name,
                            error.message
                        );
                        console.log("error = ", error);
                        dom.byId("viewDiv").style.cursor = "auto";
                    });
            }

 

查詢N多資料,走了N多彎路之后,最后找到了問題所在。

由於該功能中,map沒有直接添加FeatureLayer圖層,圖層的展示是通過MapImageLayer的方式添加的,該MapImageLayer包含對個FeatureLayer圖層。

而在更新要素屬性的時候,獲取到具體的FeatureLayer調用其refresh()方法,推測應該為用於applyEdits()的FeatureLayer並不是當前地圖用於展示的圖層,

所以調用其刷新是沒有效果的。

所以,調用當前map的MapImageLayer的refresh()方法,即可實現地圖渲染的刷新。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM