将json字符串转换为适合esri.geometry.Multipoint的字符串

时间:2014-01-31 13:40:23

标签: javascript json esri esri-javascript-api

我有一个webApi,它返回一个Json字符串

[{"MapEast":504743,"MapNorth":439624},{"MapEast":504736,"MapNorth":439622},{"MapEast":504775,"MapNorth":439722},{"MapEast":504739,"MapNorth":439738},{"MapEast":504774,"MapNorth":439715},{"MapEast":504739,"MapNorth":439734},{"MapEast":504773,"MapNorth":439711},{"MapEast":504739,"MapNorth":439728},{"MapEast":504773,"MapNorth":439705},{"MapEast":504739,"MapNorth":439724},{"MapEast":504773,"MapNorth":439699},{"MapEast":504743,"MapNorth":439718},{"MapEast":504773,"MapNorth":439694},{"MapEast":504743,"MapNorth":439713},{"MapEast":504776,"MapNorth":439688},{"MapEast":504742,"MapNorth":439708},{"MapEast":504773,"MapNorth":439680},{"MapEast":504743,"MapNorth":439703},{"MapEast":504774,"MapNorth":439674},{"MapEast":504742,"MapNorth":439698},{"MapEast":504773,"MapNorth":439668},{"MapEast":504743,"MapNorth":439693},{"MapEast":504773,"MapNorth":439663},{"MapEast":504740,"MapNorth":439688},{"MapEast":504773,"MapNorth":439658},{"MapEast":504742,"MapNorth":439679},{"MapEast":504775,"MapNorth":439653},{"MapEast":504742,"MapNorth":439676},{"MapEast":504743,"MapNorth":439670},{"MapEast":504742,"MapNorth":439665},{"MapEast":504742,"MapNorth":439660},{"MapEast":504741,"MapNorth":439656},{"MapEast":504741,"MapNorth":439650},{"MapEast":504741,"MapNorth":439645},{"MapEast":504740,"MapNorth":439640},{"MapEast":504741,"MapNorth":439634},{"MapEast":504740,"MapNorth":439631}]`

我需要弄清楚如何将这些数据用于在arcMap javascript api中创建一个图层

function updateExtent(){
    var myMultiPoint = {
        "geometry":{
            "points":[[504743,439624],[504736,439622],[504775,439722]],
            "spatialReference":27700
        },
        "symbol":{
            "color":[255,255,255,64],
            "size":12,
            "angle":0,
            "xoffset":0,
            "yoffset":0,
            "type":"esriSMS",
            "style":"esriSMSCircle",
            "outline":{
                "color":[0,0,0,255],
                "width":1,
                "type":"esriSLS",
                "style":"esriSLSSolid"
            }
        }
    };  

    var gra = new esri.Graphic(myMultiPoint);

    myMap.graphics.add(gra);

    var graExtent = esri.graphicsExtent(myMap.graphics.graphics); 
    myMap.setExtent(graExtent); 
}

我已经测试了上面的代码,它可以创建图层并且工作正常。我只需要一些关于如何从webApi获取json数据到myMultiPoint变量创建的指导。

由于

2 个答案:

答案 0 :(得分:2)

使用它将您的JSON数据转换为points数组:

var myData = JSON.parse('[{"MapEas... ...9631}]');

var points = myData.map(function(x){
    return [x.MapEast, x.MapNorth];
});

然后,您可以像这样简单地添加它:

var myMultiPoint = {
    "geometry":{
        "points":points, // Just reference the array, here.
        "spatialReference":27700
    },
    "symbol":{
        "color":[255,255,255,64],
        "size":12,
        "angle":0,
        "xoffset":0,
        "yoffset":0,
        "type":"esriSMS",
        "style":"esriSMSCircle",
        "outline":{
            "color":[0,0,0,255],
            "width":1,
            "type":"esriSLS",
            "style":"esriSLSSolid"
        }
    }
};

答案 1 :(得分:0)

您可以使用以下任意方式将 json 转换为esri.Geometry

  • JsonUtilsesri/geometry/jsonUtils)或
  • esri.geometry.fromJson方法。

以下是代码:

方法一(使用JsonUtils

require(
    ["esri/map", "esri/geometry/jsonUtils", "esri/config", "dojo/domReady!"],
    function (Map, JsonUtils, esriConfig) {

    var jsonGeometry = {"x":10,"y":20,"spatialReference":{"wkid":3857}};

    //Note: you should not use JsonUtils.fromJson(JSON.stringify(jsonGeometry))
    var geometry = JsonUtils.fromJson(jsonGeometry); 
    var graphic = new esri.Graphic(firstGeometry);
});

方法二(使用geometry.fromJson方法)

var jsonGeometry = {"x":10,"y":20,"spatialReference":{"wkid":3857}};
var geometry = esri.geometry.fromJson(jsonGeometry);
var graphic = new esri.Graphic(geometry);