如何在OpenLayers 3中运行Graticule标签?

时间:2016-04-26 09:47:34

标签: javascript dictionary label openlayers-3

我需要在 OpenLayers 3 中运行Graticule 标签。我想用角度单位显示和标记刻度标签。我刚刚发现了这个:

https://github.com/Brictarus/ol3/blob/d41eb87204e76cbf99d61915eb89b1c16c4a4e05/examples/graticule.js

但是我无法在我的HTML中使用它:

var polohaMysky = new ol.control.MousePosition({
  coordinateFormat: ol.coordinate.createStringXY(4),
  //projection: "EPSG:4326",
  className: "suradnice",
  target: document.getElementById("suradnice_div"),
                undefinedHTML: '0000'
})
            //------//
            var mierka = new ol.control.ScaleLine();
            var nahlad = new ol.control.OverviewMap();
            var a1 = new ol.source.OSM();
      var map = new ol.Map({
        renderer: 'canvas',
          // Zobrazenie súradníc myšky
        controls: ol.control.defaults({
            attributions: false
        }).extend([polohaMysky, mierka, nahlad]),
        layers: [
          new ol.layer.Tile({
            source: a1
          })
        ],
        target: 'map',
        view: new ol.View({
            //projection: 'EPSG:4326',
            center: ol.proj.fromLonLat([4.8, 47.75]),
          zoom: 3
        })
      });
           var lonFormatter = function(lon) {
  var formattedLon = Math.abs(Math.round(lon * 100) / 100);
  formattedLon += (lon < 0) ? 'W' : ((lon > 0) ? 'E' : '');
  return formattedLon;
};

var latFormatter = function(lat) {
  var formattedLat = Math.abs(Math.round(lat * 100) / 100);
  formattedLat += (lat < 0) ? 'S' : ((lat > 0) ? 'N' : '');
  return formattedLat;
};

// Create the graticule component
var graticule = new ol.Graticule({
  // the style to use for the lines, optional.
  strokeStyle: new ol.style.Stroke({
    color: 'rgba(255,120,0,0.9)',
    width: 2,
    lineDash: [0.5, 4]
  }),
  showLabels: true,
  lonLabelFormatter: lonFormatter,
  lonLabelPosition: 0.05,
  latLabelFormatter: latFormatter,
  latLabelPosition: 0.95
});
graticule.setMap(map);

当我运行此代码时,我只看到没有标签的刻度。你知道什么是错的吗? 感谢您的帮助

2 个答案:

答案 0 :(得分:0)

latest document开始,似乎OL3还不支持标签

答案 1 :(得分:0)

OpenLayers 3不支持 Graticule标签(OL3的最新版本为3.20.1)。请参见OL3 API参考:https://openlayers.org/en/v3.20.1/apidoc/ol.Graticule.html

如果需要显示标线标签,请考虑升级到支持标线标签的OpenLayers 5。请参见OL5 API参考:https://openlayers.org/en/latest/apidoc/module-ol_Graticule.html