打开图层标签不工作



我一直在尝试让openlayers标签功能工作,并产生了以下示例:

<html>
<head>
  <title>OpenLayers Example</title>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
</head>
<body>
  <div style="width:100%; height:100%" id="map"></div>
  <script>
    var map = new OpenLayers.Map('map');
    var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
    map.addLayer(wms);
    // Default polygon style
    var polygonStyle = 
    OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
    polygonStyle.strokeColor = "#800000";
    polygonStyle.fillColor = "#800080";
    polygonStyle.fillOpacity = 0.2;
    polygonStyle.strokeWidth = 1;
    polygonStyle.label = "Label:${label}";
    polygonStyle.labelOutlineColor = "white";
    polygonStyle.labelOutlineWidth = 3;
    var smap = new OpenLayers.StyleMap({"default": polygonStyle});
    var veclayer = new OpenLayers.Layer.Vector("Survey Locations", {"styleMap": smap});
    map.addLayer(veclayer);
    var data = {"type":"Polygon","coordinates":[[
    [-2.07362131225228,52.0329916851734],
    [-2.07096056091493,52.0228522264397],
    [-2.05061868774548,52.0156687188299],
    [-2.04280809509186,52.0210036398637],
    [-2.02804521667506,52.0231163039992],
    [-2.01748804200037,52.0300345805213],
    [-2.01645807373352,52.0464545997404],
    [-2.02589944946666,52.0529998067114],
    [-2.04194978821027,52.0554276235705],
    [-2.06023172485491,52.0455044093648],
    [-2.07362131225228,52.0329916851734]]]};
    var gson = new OpenLayers.Format.GeoJSON();
    var GEO = gson.read(data, "Geometry");
    var EPSG4326 = new OpenLayers.Projection("EPSG:4326");
    GEO = GEO.transform(EPSG4326, map.getProjectionObject());
    var locname="First label";
    var FEA = new OpenLayers.Feature.Vector(GEO, {"label":locname}, polygonStyle);
    veclayer.addFeatures([FEA]);
    map.zoomToExtent(GEO.getBounds(),false);
    data= {"type":"Polygon","coordinates":[[[-2.04514962074064,52.0403793945411],
    [-2.03057040393828,52.0403841112724],
    [-2.03057659173109,52.0493747022699],
    [-2.04515873420745,52.049369984023],
    [-2.04514962074064,52.0403793945411]]]}
    var GEO = gson.read(data, "Geometry");
    var EPSG4326 = new OpenLayers.Projection("EPSG:4326");
    GEO = GEO.transform(EPSG4326, map.getProjectionObject());
    var locname="Second label";
    var FEA = new OpenLayers.Feature.Vector(GEO, {"label":locname}, polygonStyle);
    veclayer.addFeatures([FEA]);
  </script>
</body>
</html>

代码可以工作,但是多边形上的标签显示为label:${label},而不是将变量解释为label: First label等。

当它集成到更大的应用程序中时,第一个标签不能正确解释,而第二个标签可以。我怀疑这可能是一个图层初始化问题,但不知道如何解决这个问题。

任何帮助将不胜感激!!

您是否尝试在声明功能后添加属性作为对象?例如:

有限元分析。属性= {标签:locname}

它是这样工作的,看看下面的变化:

<html>
<head>
  <title>OpenLayers Example</title>
<script src="http://openlayers.org/api/OpenLayers.js"></script>
</head>
<body>
  <div style="width:100%; height:100%" id="map"></div>
  <script>
    var map = new OpenLayers.Map('map');
    var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
    map.addLayer(wms);
    // Default polygon style
    var polygonStyle = 
    OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
    polygonStyle.strokeColor = "#800000";
    polygonStyle.fillColor = "#800080";
    polygonStyle.fillOpacity = 0.2;
    polygonStyle.strokeWidth = 1;
    polygonStyle.label = "Label:${label}";
    polygonStyle.labelOutlineColor = "white";
    polygonStyle.labelOutlineWidth = 3;
    var smap = new OpenLayers.StyleMap({"default": polygonStyle});
    var veclayer = new OpenLayers.Layer.Vector("Survey Locations", {"styleMap": smap});
    map.addLayer(veclayer);
    var data = {"type":"Polygon","coordinates":[[
    [-2.07362131225228,52.0329916851734],
    [-2.07096056091493,52.0228522264397],
    [-2.05061868774548,52.0156687188299],
    [-2.04280809509186,52.0210036398637],
    [-2.02804521667506,52.0231163039992],
    [-2.01748804200037,52.0300345805213],
    [-2.01645807373352,52.0464545997404],
    [-2.02589944946666,52.0529998067114],
    [-2.04194978821027,52.0554276235705],
    [-2.06023172485491,52.0455044093648],
    [-2.07362131225228,52.0329916851734]]]};
    var gson = new OpenLayers.Format.GeoJSON();
    var GEO = gson.read(data, "Geometry");
    var EPSG4326 = new OpenLayers.Projection("EPSG:4326");
    GEO = GEO.transform(EPSG4326, map.getProjectionObject());
    var locname="First label";
    var FEA = new OpenLayers.Feature.Vector(GEO);
    FEA.attributes = {
    label:locname
    }
    veclayer.addFeatures([FEA]);
    map.zoomToExtent(GEO.getBounds(),false);
    data= {"type":"Polygon","coordinates":[[[-2.04514962074064,52.0403793945411],
    [-2.03057040393828,52.0403841112724],
    [-2.03057659173109,52.0493747022699],
    [-2.04515873420745,52.049369984023],
    [-2.04514962074064,52.0403793945411]]]}
    var GEO = gson.read(data, "Geometry");
    var EPSG4326 = new OpenLayers.Projection("EPSG:4326");
    GEO = GEO.transform(EPSG4326, map.getProjectionObject());
    var locname="Second label";
    var FEA = new OpenLayers.Feature.Vector(GEO);
    FEA.attributes = {
        label: locname
    }
    veclayer.addFeatures([FEA]);
  </script>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新