Openlayer 2将标签设置为矢量线的中间

时间:2016-07-11 07:14:44

标签: javascript google-maps openlayers

伙计我使用Openlayer 2在Google地图上创建图层。

我使用Openlayer 2绘制矢量线,我想在该行的中间设置标签。

下面是我的代码。

 var ol = new OpenLayers.Layer.OSM();
            var vector = new OpenLayers.Layer.Vector( "Lines" );

            for (var i = 0; i < fromtoLatlngObject.data.length; i++) {

                var start_point = new OpenLayers.Geometry.Point(fromtoLatlngObject.data[i].from.lng,fromtoLatlngObject.data[i].from.lat);
                var end_point = new OpenLayers.Geometry.Point(fromtoLatlngObject.data[i].to.lng,fromtoLatlngObject.data[i].to.lat);

                var firstLine = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString([start_point, end_point]).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")));

                vector.addFeatures([
                    firstLine
                ]);
            }

            var selected_polygon_style = {
                strokeWidth: 2,
                strokeColor: 'blue',
                fontColor: "red",
                fontSize: "16px",
                fontFamily: "arial, monospace",
                fontWeight: "bold",
                fontColor: "green",
                label: "To: test",
                labelAlign: "cm"
            };

            vector.style = selected_polygon_style;
            map.addLayers([ol,vector]);

            var markers = new OpenLayers.Layer.Markers( "Marker" );
            map.addLayer(markers);

            for (var i = 0; i < latlngObj.data.length; i++) {
                addMarker(latlngObj,map,markers,i);
            }

        }

根据Openlayer 2 Document,我们可以将labelAlign设置为。

labelAlign: "cm",

我设置了labelAlign但不起作用,你也可以签入文件。

http://dev.openlayers.org/apidocs/files/OpenLayers/Feature/Vector-js.html

或者你也可以在Openlayer 2给出的演示中看到。

http://dev.openlayers.org/examples/vector-features-with-text.html

由于

0 个答案:

没有答案