openlayers / ol2

OpenLayers v2 - deprecated!
Other
1.47k stars 771 forks source link

Vector features disappear when viewport crosses anti-meridian #648

Open stephend opened 12 years ago

stephend commented 12 years ago

You can view this in the test case here: http://cloud.github.com/downloads/opennewzealand/linz2osm/openlayers-bug-demo.html

You can see the polygon over New Zealand. Pan the map east, and once the antimeridian appears in the viewport, it disappears. If you keep panning east, once the antimeridian is in the left half of the viewport, the polygon appears again.

For reference, this appears in both Firefox 13.0.1 and Chrome 20.0.1132.57 on Ubuntu Linux 12.04.

saraid commented 12 years ago

FYI, the reason for this is that bounds are arbitrary numbers and cover an infinite plane, rather than wrapping around at some arbitrary point. The workaround I've found in reading threads has been to duplicate your vector features so that they exist on both sides of the anti-meridian. I haven't come up with a solution I'm happy with yet.

In my own project, I started using Map.restrictedExtent, which "fixes" it by not allowing the situation to exist in return for losing the ability to usefully display things along that line.

Here's my workaround:

var left = vectorLayer.getExtent().left;
var difference = (Math.abs(left)/left) * vectorLayer.maxExtent.getWidth();
var features = _(vectorLayer.features).chain()
    .select(function(f) { return !f.onScreen(); })
    .map(function(f) {
        return new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(f.geometry.x + difference,
                                          f.geometry.y),
            f.attributes, f.style);
    }).value();
vectorLayer.addFeatures(features);