简体   繁体   English

如何画一条线将标签连接到OpenLayers3上的位置

[英]How to draw a line connecting a label to its position on OpenLayers3

I need to draw a line connecting a point to its label. 我需要画一条线将一个点连接到它的标签。 Like this: 像这样:

在此处输入图片说明

The 'Y' has coordinates, the label is in a different position on the screen by using offSet. “ Y”具有坐标,通过使用offSet,标签在屏幕上的不同位置。 I also can drag the label around by changing its offset, and I need the line to adapt when I do that so they remain connected. 我还可以通过更改标签的偏移量来拖动标签,这样做时我需要调整线条以使其保持连接状态。

Is there any way to draw a line on the map without providing coordinates to it? 有没有办法在地图上画线而不提供坐标? Because I only have one set of coordinates and an offset. 因为我只有一组坐标和一个偏移量。

This is how I created the point: 这就是我创造观点的方式:

    configLabel(offsetx,offsety,label){
    labelStyle = new ol.style.Style({
        text: new ol.style.Text({
            font: '12px Calibri,sans-serif',
            fill: new ol.style.Fill({ color: color }),
            stroke: new ol.style.Stroke({
            color: '#fff', width: 2
            }),
            text: label,
            offsetY: offsety,
            offsetX: offsetx
        })
    });
}

pointStyle = new ol.style.Style({
    image: new ol.style.Icon({
        anchor: [0.5, 25],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        src: 'img/point.ico'
    })
});

var point = new ol.Feature(new ol.geom.Point(coord));
point.setStyle(pointStyle);
layerSource.addFeature(point);

var label1 = new ol.Feature(new ol.geom.Point(coord));
var style = configLabel(0,15, 'line1');
label1.setStyle(style);
layerSource.addFeature(label1);

var label2 = new ol.Feature(new ol.geom.Point(coord));
var style = configLabel(0,30, 'line2');
label2.setStyle(style);
layerSource.addFeature(label2);

var label3 = new ol.Feature(new ol.geom.Point(coord));
var style = configLabel(0,45, 'line3');
label3.setStyle(style);
layerSource.addFeature(label3);

var label4 = new ol.Feature(new ol.geom.Point(coord));
var style = configLabel(0,60, 'line4');
label4.setStyle(style);
layerSource.addFeature(label4);

Thanks a lot! 非常感谢!

I don't know of any way to draw lines in by pixel values rather than coordinate values in OpenLayers 3 styles. 我不知道有什么方法可以通过像素值绘制线条,而不是OpenLayers 3样式中的坐标值。 You could however use a style function , which takes the resolution as an argument and use the resolution to create a geometry from the desired pixel offsets. 但是,您可以使用样式函数 ,该函数将分辨率作为参数,并使用分辨率根据所需的像素偏移量创建几何。

Since the resolution is provided as the number of map units per pixel, finding a position from a given pixel is trivial: You multiply the pixel offset with the resolution. 由于提供的分辨率是每个像素的映射单位数,因此从给定像素中查找位置很简单:将像素偏移量乘以分辨率。

A runnable example can be found in the snippet below. 下面的代码片段提供了一个可运行的示例。

 var feature = new ol.Feature(new ol.geom.Point([0, 0])); var source = new ol.source.Vector({ features: [feature] }); var fill = new ol.style.Fill({ color: 'rgba(255,255,255,1)' }); var stroke = new ol.style.Stroke({ color: '#3399CC', width: 1.25 }); var pointStyle = new ol.style.Style({ image: new ol.style.Circle({ fill: fill, stroke: stroke, radius: 5, }) }) var lineLabelStyle = function(pointCoord, offsetX, offsetY, resolution, text) { var labelCoord = [ pointCoord[0] + offsetX * resolution, pointCoord[1] - offsetY * resolution, ]; return [ new ol.style.Style({ stroke: stroke, geometry: new ol.geom.LineString([pointCoord, labelCoord]) }), new ol.style.Style({ text: new ol.style.Text({ text: text, textAlign: "left", offsetY: offsetY, offsetX: offsetX, }) }) ]; }; var styleFunction = function(feature, resolution) { var pointCoord = feature.getGeometry().getCoordinates() return [pointStyle].concat( lineLabelStyle(pointCoord, 50, -10, resolution, "Label 1"), lineLabelStyle(pointCoord, 50, 5, resolution, "Label 2"), lineLabelStyle(pointCoord, 50, 20, resolution, "Label 3") ) } var map = new ol.Map({ layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }), new ol.layer.Vector({ source: source, style: styleFunction }) ], target: 'map', view: new ol.View({ center: [0, 0], zoom: 2 }) }); 
 <link href="http://openlayers.org/en/v3.10.1/css/ol.css" rel="stylesheet" /> <script src="http://openlayers.org/en/v3.10.1/build/ol.js"></script> <div id="map" class="map"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM