简体   繁体   中英

Can't save and then restore Graphic

I use sketchViewModel for editing layer. I have a next logic:

  • uploading basic model;
  • editing model;
  • saving edit model to localStorage
  • uploading model from localStorage.

When I upload model from local storage and add to graphicLayers, i have errors:

[esri.core.Accessor] Accessor#set Invalid property value, value needs to be one of 
'esri.geometry.Extent', 'esri.geometry.Multipoint', 'esri.geometry.Point', 'esri.geometry.Polyline', 
'esri.geometry.Polygon', or a plain object that can autocast (having .type = 'extent', 'multipoint',
'point', 'polyline', 'polygon')

[esri.core.Accessor] Accessor#set Invalid property value, value needs to be one of 
'esri.symbols.SimpleFillSymbol', 'esri.symbols.PictureFillSymbol', 'esri.symbols.PictureMarkerSymbol', 
'esri.symbols.SimpleLineSymbol', 'esri.symbols.SimpleMarkerSymbol', 'esri.symbols.TextSymbol', 
'esri.symbols.LabelSymbol3D', 'esri.symbols.LineSymbol3D', 'esri.symbols.MeshSymbol3D', 
'esri.symbols.PointSymbol3D', 'esri.symbols.PolygonSymbol3D', 'esri.symbols.WebStyleSymbol', 
'esri.symbols.CIMSymbol', or a plain object that can autocast (having .type = 'simple-fill', 'picture-
fill', 'picture-marker', 'simple-line', 'simple-marker', 'text', 'label-3d', 'line-3d', 'mesh-3d', 
'point-3d', 'polygon-3d', 'web-style', 'cim')

Here is my code:

sketchViewModel.on("update", checkGraphicUpdate);

function checkGraphicUpdate(evt) {
    if(evt.state === 'complete'){
        // dispatchRecentChanges(geometryGraphics);
        localStorage.setItem('features', geometryGraphics.toJSON())
    }
}

if(uploadView){
    graphicsLayer.removeAll();

    JSON.parse(localStorage.feautures).forEach(
        function(featureJson){
            graphicsLayer.add(new Graphic(featureJson))}
    );
}

Here is an example of an element in JSON in LocalStorage:

{"geometry":{"spatialReference":{"wkid":4326},"paths":[[[-111.3, 52.68], [-98,
 49.5], [-93.94, 29.89]]]},"symbol":{"type":"esriSLS","color":
[0,255,0,255],"width":4,"style":"esriSLSSolid"},"attributes":
{"DATA_SOURSE":"3","AVERAGE_DEPTH":"1.2","MATERIAL":"14","PLACINGFORM":"2","MEAS
UREDLENGTH":"12.4","DIAMETER":"6","STREETNAME":"אבן 
עזרא","DIAMETERUNIT":"'אינצ","STATUS":"1","LOCATION":"13","INSTALLYEAR":"2018","
Title":"Water_Pipe_Section [F3FA]","PURPOSE":"1"},"popupTemplate":
{"popupElements":[{"type":"fields","fieldInfos":
[{"fieldName":"DATA_SOURSE","visible":true},
{"fieldName":"AVERAGE_DEPTH","visible":true},
{"fieldName":"MATERIAL","visible":true},
{"fieldName":"PLACINGFORM","visible":true},
{"fieldName":"MEASUREDLENGTH","visible":true},
{"fieldName":"DIAMETER","visible":true},
{"fieldName":"STREETNAME","visible":true},
{"fieldName":"DIAMETERUNIT","visible":true},
{"fieldName":"STATUS","visible":true},{"fieldName":"LOCATION","visible":true},
{"fieldName":"INSTALLYEAR","visible":true},{"fieldName":"Title","visible":true},
{"fieldName":"PURPOSE","visible":true}]}],"title":"{Title}"}}

I understand that this is due to errors in the stored data. But how to save this data, then to extract without errors?

This error occurred because geometry.type and symbol.type were not saved in JSON. The solution is to check if((data.symbol ['type'] === "esriSLS")) then extends of new Polyline() and explicitly specify symbol: {type: "simple-line"} in new Graphic().

Code:

if (data.symbol['type'] === "esriSLS") {

        const {type, fieldInfos} = popupElements[0],
            {paths} = geometry;

        const pLine = new Polyline({spatialReference, paths});

        const symbol = {
            type: "simple-line",
            width: width,
            color: color
        };

        return new Graphic({
            geometry: pLine,
            symbol: symbol,
            attributes: attributes,
            popupTemplate: {
                title: "{Title}",
                content: [{type, fieldInfos}]
            }
        });
    }

In your code above I am not sure whether geometryGraphics refers to an array of graphics or a single one. That being said, if you have a Graphic object, the best way to serialize/deserialize it is using toJSON() and fromJSON() . That way you don't have to worry about the internal JSON representation.

var graphic = new Graphic({
  geometry: ...,
  symbol: ...,
  ...
});

var json = graphic.toJSON();
var newGraphic = Graphic.fromJSON(json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM