ESRI ArcGIS Javascript:来自 JSON 的多边形不工作
ESRI ArcGIS Javascript : Polygon from JSON not working
如果您引用图像、JSON 字符串和下面的代码片段,它概述了代码、控制台输出和尝试使用以下方法绘制多边形(或任何 geometry/graphic)的预期行为ESRI ArcGIS Javascript API。不确定发生了什么....请帮忙!
JSON 字符串:
{"geometry":{"rings":[[[-91.89013671874848,38.03029444608522],[-91.653930664061,38.00865683368494],[-91.64843749999851,38.00432854459864],[-91.5935058593735,37.93070854451552],[ -91.577026367186,37.88303274722063],[-91.577026367186,37.79192956603227],[-91.631958007811,37.73982010276601],[-91.70886230468598,37.73547599031287],[-91.763793945311,37.76587942393493],[-91.85168457031098,37.85701474874939],[-91.88464355468598,37.9956711998967],[- 91.89013671874848,38.03029444608522]]],"spatialReference":{"wkid":4326}},"symbol":{"color":[0,0,0,64],"outline":{"color":[0,0,0,255],"width":1,"type":"esriSLS","style":"esriSLSSolid"} ,"type":"esriSFS","style":"esriSFSSolid"}}
向地图添加形状的代码:
function createFromJSON(JSONText){
console.log("In Create Function");
dojo.disconnect(handle);
var jsontext = JSON.parse(JSONText);
var polygon = new esri.geometry.Polygon(jsontext);
console.log("Here is the polygon object:");
console.log(polygon);
console.log("Now drawing polygon");
map.graphics.add(new Graphic(polygon, new SimpleFillSymbol()));
console.log("Polygon should be there");
}
图像中显示的 JSON 字符串用于 Graphic
对象而不是几何体。如您所见,它包含 geometry
和 symbol
,如果将其传递给 Graphic,它将起作用。
map.graphics.add(new Graphic(jsontext));
或者,如果您只想要多边形,那么您的代码应该是这样的。
var polygon = new esri.geometry.Polygon(jsontext.geometry);
此外,不要将传统风格和 AMD 风格结合在一起。
正如 T Kambi 所说,您的字符串用于图形而非几何,但我将展示一些在 json 和 esriGeometry 之间进行转换的想法。
您可以使用以下任一方式将 json 转换为 esri.Geometry
:
JsonUtils
(esri/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);
如果您引用图像、JSON 字符串和下面的代码片段,它概述了代码、控制台输出和尝试使用以下方法绘制多边形(或任何 geometry/graphic)的预期行为ESRI ArcGIS Javascript API。不确定发生了什么....请帮忙!
JSON 字符串:
{"geometry":{"rings":[[[-91.89013671874848,38.03029444608522],[-91.653930664061,38.00865683368494],[-91.64843749999851,38.00432854459864],[-91.5935058593735,37.93070854451552],[ -91.577026367186,37.88303274722063],[-91.577026367186,37.79192956603227],[-91.631958007811,37.73982010276601],[-91.70886230468598,37.73547599031287],[-91.763793945311,37.76587942393493],[-91.85168457031098,37.85701474874939],[-91.88464355468598,37.9956711998967],[- 91.89013671874848,38.03029444608522]]],"spatialReference":{"wkid":4326}},"symbol":{"color":[0,0,0,64],"outline":{"color":[0,0,0,255],"width":1,"type":"esriSLS","style":"esriSLSSolid"} ,"type":"esriSFS","style":"esriSFSSolid"}}
向地图添加形状的代码:
function createFromJSON(JSONText){
console.log("In Create Function");
dojo.disconnect(handle);
var jsontext = JSON.parse(JSONText);
var polygon = new esri.geometry.Polygon(jsontext);
console.log("Here is the polygon object:");
console.log(polygon);
console.log("Now drawing polygon");
map.graphics.add(new Graphic(polygon, new SimpleFillSymbol()));
console.log("Polygon should be there");
}
图像中显示的 JSON 字符串用于 Graphic
对象而不是几何体。如您所见,它包含 geometry
和 symbol
,如果将其传递给 Graphic,它将起作用。
map.graphics.add(new Graphic(jsontext));
或者,如果您只想要多边形,那么您的代码应该是这样的。
var polygon = new esri.geometry.Polygon(jsontext.geometry);
此外,不要将传统风格和 AMD 风格结合在一起。
正如 T Kambi 所说,您的字符串用于图形而非几何,但我将展示一些在 json 和 esriGeometry 之间进行转换的想法。
您可以使用以下任一方式将 json 转换为 esri.Geometry
:
JsonUtils
(esri/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);