如何标记以编程方式生成的要素图层?

时间:2014-07-17 12:43:19

标签: javascript arcgis arcgis-js-api

我是ESRI ArcGIS API for Javascript的新手,我正在尝试以编程方式生成要素图层,然后为其添加标签图层。功能层似乎有效,但标签层不提供输出(也没有错误)。有人能告诉我我做错了吗?

jsfiddle:http://jsfiddle.net/9X46z/

相关的javascript:

var layerDefinition = {
    "geometryType": "esriGeometryPoint",
    "fields": [
        {
            "name": "__OBJECTID",
            "alias": "__OBJECTID",
            "type": "esriFieldTypeOID",
        }, {
            "name": "testfield",
            "alias": "testfield",
            "type": "esriFieldTypeString"
        }
    ]
}

var featureCollection = {
    layerDefinition: layerDefinition,
    featureSet: null
};

var fl = new FeatureLayer(featureCollection, {
    id: "featurelayer",
    outFields: ["testfield"]
})

var m = new esri.Graphic(new esri.geometry.Point(-77, 40), pushpin);
m.attributes = {
    "testfield": "test value"
};

fl.add(m);

map.addLayer(fl);

// create a text symbol to define the style of labels
var testLabel = new TextSymbol().setColor(new Color("#666666"));
testLabel.font.setSize("14pt");
testLabel.font.setFamily("arial");
testLabelRenderer = new SimpleRenderer(testLabel);

var labels = new LabelLayer({
    id: "labels"
});
labels.addFeatureLayer(fl, testLabelRenderer, "{testfield}");
// add the label layer to the map
map.addLayer(labels);

2 个答案:

答案 0 :(得分:2)

这是一个更新版本,只有一处更改:http://jsfiddle.net/2WccC/1/

var m = new esri.Graphic(new esri.geometry.Point(-77, 40), pushpin);

var m = new esri.Graphic(esri.geometry.geographicToWebMercator(new esri.geometry.Point(-77, 40)), pushpin);

答案 1 :(得分:1)

尝试在创建地图时将Map的showLabels选项设置为true 这默认是假的。     map = new Map(“mapDiv”,{showLabels:true});