在OpenLayers 3中格式化MousePosition控件输出

时间:2014-11-12 06:15:09

标签: openlayers-3

我使用以下控件在OpenLayers 3中显示鼠标位置

var mousePositionControl = new ol.control.MousePosition({
    coordinateFormat: ol.coordinate.createStringXY(2),
    projection: 'EPSG:4326',   
    undefinedHTML: ' '
});

但结果显示鼠标位置为Lon,Lat而不是Lat,Lon。

这里是jsfiddle example

如何撤销订单以使其成为Lat,Lon?

5 个答案:

答案 0 :(得分:8)

添加各种控件(包括Lat,Long)对我来说是有用的:

var controls = [
  new ol.control.Attribution(),
  new ol.control.MousePosition({
    projection: 'EPSG:4326',
    coordinateFormat: function(coordinate) {
      return ol.coordinate.format(coordinate, '{y}, {x}', 4);
    }
  }),
  new ol.control.ScaleLine(),
  new ol.control.Zoom(),
  new ol.control.ZoomSlider(),
  new ol.control.ZoomToExtent(),
  new ol.control.FullScreen()
];
(从the book of openlayers 3修改)

答案 1 :(得分:4)

您更改了coordinateFormat - "标准功能"自定义函数:

var myFormat = function(dgts)
{
  return (
    function(coord1) {
        var coord2 = [coord1[1], coord1[0]]; 
      return ol.coordinate.toStringXY(coord2,dgts);
  });        
}

var mousePositionControl = new ol.control.MousePosition({
    coordinateFormat: myFormat(2), // <--- change here
    projection: 'EPSG:4326',
    className: 'custom-mouse-position',
    target: document.getElementById('mouse-position'),
    undefinedHTML: '&nbsp;'
});

查看修改后的fiddle

答案 2 :(得分:3)

替代方案:

var template = 'LatLon: {y}, {x}';

var mousePositionControl = new ol.control.MousePosition({
    coordinateFormat: function(coord) {return ol.coordinate.format(coord, template, 2);},
    projection: 'EPSG:4326',   
    undefinedHTML: '&nbsp;'
    });

答案 3 :(得分:2)

还有助于以度,分,秒显示:

Class A 
{
    public int data;
}

Class B : Class A { }

Class C : Class A { }

Class D : Class A { }

.
.
.

Class G<T> 
{
    T field
}

main()
{
    object t = CallOuterService();
}

答案 4 :(得分:0)

适用于OpenLayers 3.7.0。使用proj4js将坐标重新投影到不同的投影,因为地图视图位于&#39; EGPS:3857&#39;:

&#13;
&#13;
var proj1 = proj4.defs('EPSG:4326');
var proj2 = proj4.defs('EPSG:3857');

var myFormat = function(digits) {
  return (
    function(originalCoordinates) {
      var reprojectedCoordinates = proj4(proj2, proj1).forward(originalCoordinates);
      var switchedCoordinates = [reprojectedCoordinates[1], reprojectedCoordinates[0]];
      return ol.coordinate.toStringXY(switchedCoordinates, digits);
    }
  );
}

var mousePositionControl = new ol.control.MousePosition({
  coordinateFormat: mojFormat(10),
  projection: 'ESPG:4326',
  undefinedHTML: '&nbsp'
});
// map.addControl(mousePositionControl);  //equivalent to setMap
mousePositionControl.setMap(map);
&#13;
&#13;
&#13;