2014-11-12 12 views
7

ich die Mausposition in Openlayers zeigt 3 mit der folgenden SteuerFormatierung der MousePosition Steuerausgang in Openlayers 3

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

Aber das Ergebnis zeigt die Mausposition als Lon, Lat statt Lat, Lon.

Hier ist ein jsfiddle example.

Wie kann ich die Reihenfolge umkehren, so dass es sich um Lat, Lon handelt?

Antwort

8

Was funktioniert für mich ein hinzufügen Vielzahl von Kontrollen einschließlich Lat, Long ist:

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() 
 
];
(modifiziert nach the book of openlayers 3)

4

Sie Ihre coordinateFormat ändern - "Standardfunktion" auf eine benutzerdefinierte Funktion:

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;' 
}); 

sehen Ihre fiddle modifizierte

3

Eine Alternative:

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;' 
    }); 
2

auch hilfreich in Grad, Minuten, Sekunden anzuzeigen:

controls: ol.control.defaults().extend([ 
     new ol.control.ScaleLine({ 
      units: 'nautical' 
     }), 
     new ol.control.MousePosition({ 
      coordinateFormat: function(coord) { 
       return ol.coordinate.toStringHDMS(coord); 
      }, 
      projection: 'EPSG:4326', 
      className: 'custom-mouse-position', 
      target: document.getElementById('mouse-position'), 
      undefinedHTML: '&nbsp;' 
     }) 
    ]), 
0

Arbeiten in Openlayers 3.7.0. proj4js Koordinaten auf eine andere Projektions aufgrund Kartenansicht auf Reprojizier in sein 'EGPS: 3857':

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);