[GIS] Get correct coordinates of feature when projection is wrapped around dateline / OpenLayers 3

openlayers

When I use a wrapped world projection the retrival of feature coordinates doesn't get the right "repitition" of the world, if it's not the center one. In detail:

In OpenLayers 3 I have a vectormap which is wrapped around the dateline:

new ol.source.Vector({ ... wrapX: true});

When I fetch the features at an event pixel via

var feature = map.world.forEachFeatureAtPixel(pixel, function(feature, layer) {
  return feature;
});

The coordinates I get via

var coords = feature.getGeometry().getCoordinates()

… are always the same no matter in which "repetition" of the wrapped world I hover. Then I position an ol.Overlay via

overlay.setPosition(coords);

And this positioning also doesn't detect the repitition, which leads to the behaviour, that the overlay is positioned off the view, when the "central world" is panned of the screen.

EDIT: Actually the problem can be seen in this OL3 example:
http://openlayers.org/en/v3.6.0/examples/icon.html?q=overlay … just zoom out until the icon appears again to the left or right and click on it. The popover will appear in the central version.

Best Answer

I know you have found a workaround, but here's a solution that will work for you.

Basically, you want to your offset to the 'central world', or the -180 to 180 range by normalizing:

// Inside map.on('click', function (evt) {

/** Find Offset **/
var clickedon = map.getCoordinateFromPixel(evt.pixel)
var offset = Math.floor((clickedon[0] / 40075016.68) + 0.5);

// Popup handling.

Then you want to apply the offset to the coordinates where you're showing the popup:

/** Add offset **/
coord[0] += (offset * 20037508.34 * 2);

Check the jsfiddle for full working code.

Related Question