OpenLayers 5 RegularShape功能从指定坐标偏移

时间:2019-02-05 09:50:29

标签: openlayers openlayers-5

我正在将自定义标记放置在矢量层上,但是坐标点距地图上边缘的距离越低,标记形状从该点向上偏移的程度就越大。

我正在使用不同的地图投影(EPSG:5514),所以那里可能存在连接。

我需要知道的是,代码中要进行哪些更改以使标记形状位于指定坐标的中心。

代码的相关部分:

var stroke = new ol.style.Stroke({color: 'red', width: 1});
var fill = new ol.style.Fill({color: 'red'});

function styleFunction(feature, text) {
    feature.displayText = text;
    return [
        new ol.style.Style({
            image: new ol.style.RegularShape({
                fill: fill,
                stroke: stroke,
                points: 3,
                radius: 8,
                rotation: 0,
                angle: 0
            }),
            text: new ol.style.Text({
                font: '14px Calibri,sans-serif',
                fill: new ol.style.Fill({ color: markerTextColor }),
                stroke: new ol.style.Stroke({
                  color: textcolor_DKM, width: 1
                }),
                textAlign: 'left',
                offsetX: 10,
                offsetY: -2,
                text: text
              })
        })
    ];
}

var vectorSource = new ol.source.Vector();
var markerVectorLayer = new ol.layer.Vector({
    title: 'Notes',
    visible: false,
    source: vectorSource
});

var feature = new ol.Feature({
    geometry: new ol.geom.Point(
        ol.proj.fromLonLat([Number(lon), Number(lat)], 'EPSG:5514')
    )
});
feature.setStyle(styleFunction(feature, desc));
vectorSource.addFeature(feature);

如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

使用最新的proj4.js库和 OGC WKT字符串。在此处获取proj4.js:

https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.5.0/proj4.js

,然后可以使用以下代码行进行投影定义:

proj4.defs('EPSG:5514', 'PROJCS["S-JTSK / Krovak East North",GEOGCS["S-JTSK",DATUM["System_Jednotne_Trigonometricke_Site_Katastralni",SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],TOWGS84[589,76,480,0,0,0,0],AUTHORITY["EPSG","6156"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4156"]],PROJECTION["Krovak"],PARAMETER["latitude_of_center",49.5],PARAMETER["longitude_of_center",24.83333333333333],PARAMETER["azimuth",30.28813972222222],PARAMETER["pseudo_standard_parallel_1",78.5],PARAMETER["scale_factor",0.9999],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],AUTHORITY["EPSG","5514"]]');

然后,当您需要使用投影时,请使用:

var proj = ol.proj.get('EPSG:5514');