0

I have an OpenLayers.Map (map) that has an OpenLayers.Layer.Vector (map.addLayer(vectorLayer)), and I use the vectorLayer.addFeatures to add a series of OpenLayers.Feature.Vector objects to vectorLayer.

The process is a bit like this:

var map, vectorLayer;
function initialize()
{
    map = new OpenLayers.Map(/*...*/);
    vectorLayer = new OpenLayers.Layer.Vector(/*...*/);
    map.addLayer(vectorLayer);
}

function populate()
{
    arrayOfStuff.foreach(function(thing, i) // data from an AJAX call
    {
        var feature = new OpenLayers.Feature.Vector(/*...based on thing*/));
        feature.vector_type = 'marker';
        vectorLayer.addFeature(feature);
    }
}

When I then use map.setCenter to move the map, the vectors get misplaced: some shift with the map, some do not, some do both (causing duplicates), and so on. Manually panning the map even slightly immediately fixes these misplaced vectors. Unfortunately, simply calling map.pan does not (consistently) have the same effect, and vectorLayer.refresh() doesn't seem to have any effect at all.

Why is this happening and how do I get it to stop? Failing that, what can I do to force the map to fix itself (as it does when I move the map).

KRyan
  • 7,308
  • 2
  • 40
  • 68
  • I realize it makes it difficult to answer the question, but I cannot provide screenshots of the effect. – KRyan Jul 09 '13 at 21:25
  • Are you adding any other layers to the map, or just the vector layer? – djf Jul 12 '13 at 22:05
  • @djf There are other layers, I believe. I inherited this code so I'm not certain, and I can't check right now what those layers might be. – KRyan Jul 13 '13 at 01:37

1 Answers1

1

The code snippets you provided are not sufficient to diagnose the problems you're having.

It's a shot in the dark, but a common cause of misplaced markers is mixing map layers which are using different coordinate systems. OpenLayers uses a flat coordinate system based on latitude and longitude (EPSG:4326) whereas Google Maps, Microsoft Virtual Earth, Yahoo Maps, and other commercial API providers use a spherical coordinate system (EPSG:3857) also called Spherical Mercator Projection. See these related SO questions here and here.

Fortunately OpenLayers has APIs that let you transform your coordinates between various projections so that all of your layers play nicely together. The OpenLayers documentation dedicated a whole chapter to 'Spherical Mercator Projection' which contains some examples.

Community
  • 1
  • 1
djf
  • 6,592
  • 6
  • 44
  • 62
  • Sorry to get back to this so late. I'm a bit confused because the constructor of our map includes a `projection` setting. It was `EPSG:4326` and now I've made it `EPSG:3857`, but that doesn't seem to have changed anything: the markers are still correctly positioned under most circumstances, and still sometimes become misplaced when `setCenter` is used (and this still gets fixed when the map is manually moved). I'm guessing that this is because we use Google Maps and after construction use `map.getProjectionObject()` for our transforms. – KRyan Jul 16 '13 at 20:22
  • So the question becomes, what additional information would be necessary? I don't feel that dumping the entire source code is useful, nor could I in any event, but if there is anything in particular you'd find useful, I'd appreciate it. – KRyan Jul 16 '13 at 20:35
  • @KRyan Ideally you'd provide a stripped-down version of the code which still exhibits the bug as well as a recipe to reliably reproduce the problem. I realize this is a lot easier said than done. Maybe the full version of the `initialize` function could help. Or even more basic, what version of OpenLayers are you using? Does the problem occur in all browsers or just some? Does the problem also occur if you remove all layers except the vector layer? – djf Jul 16 '13 at 22:13