Jose Hermosilla Rodrigo
Jose Hermosilla Rodrigo

Reputation: 3683

Why openlayers is returning projected coordinates on map click?

I'm involved in a map project with ol3 (v 3.10) I changed the projection of my map(I guess) to "epsg 4258" wich is ETRS89.

var customExtent = [-0.6172644,39.3868231,-0.43508204,39.44951748];
var etrs89 = new ol.proj.Projection({
      code: 'EPSG:4258',
      // The extent is used to determine zoom level 0. Recommended values for a
      // projection's validity extent can be found at http://epsg.io/.
      extent: customExtent,
      units: 'degrees'
    });

Now I setup my map with this projection.

var map = new ol.Map({
      controls: ol.control.defaults({
            attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
              collapsible: false,
              units: 'degrees'
            })
       }).extend([mousePositionControl,new ol.control.FullScreen()]),
       target: 'map',
       view: new ol.View({
            center: ol.proj.fromLonLat([-0.4685, 39.4315]),
            zoom: 14           
       }),
       layers: [
         ortofoto,
         muni_torrent
       ]
});

An finally the click listener looks like:

var onclick = function(e){
    alert(e.coordinate);
};
map.on('click', onclick);

The output i'm getting is:

[-53391.90122271185, 4785457.359197952]

but I expected something like:

[-0.4, 39.9]

I also tried the following with the same result:

var onclick = function(e){
    var lonlat = map.getCoordinateFromPixel(e.pixel)
    alert('longitud: ' + lonlat[0]+ '; latitud: ' + lonlat[1]);
};

Looks like click event doesn't output the coordinates in map's projection. Some ideas? Any help would be appreciated.

Upvotes: 1

Views: 589

Answers (2)

bartvde
bartvde

Reputation: 2126

The default view is EPSG:3857 (spherical mercator) so that is what you're seeing as coordinates.

You need to specify EPSG:4258 as your view projection. See: http://openlayers.org/en/v3.10.1/examples/sphere-mollweide.html?q=mollweide for an example on how to specify a custom view projection.

Upvotes: 2

sneha
sneha

Reputation: 5

coordsDiv.textContent = 'lat: ' + Math.round(event.latLng.lat()) + ', ' + 'lng: ' + Math.round(event.latLng.lng());

Upvotes: -1

Related Questions