简体   繁体   English

OpenLayers.Map setCenter导致OpenLayers.Vector元素放错位置

[英]OpenLayers.Map setCenter causes misplaced OpenLayers.Vector elements

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 . 我有一个OpenLayers.Mapmap有一个) OpenLayers.Layer.Vectormap.addLayer(vectorLayer)我用的是vectorLayer.addFeatures添加了一系列OpenLayers.Feature.Vector反对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. 然后,当我使用map.setCenter移动地图时,矢量会放错位置:有些会随地图移动,有些则不会,有些会同时发生(导致重复),依此类推。 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. 不幸的是,简单地调用map.pan并不会(始终)具有相同的效果,而vectorLayer.refresh()似乎根本没有任何效果。

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). 失败的话,我该怎么做才能迫使地图自行修复(就像我移动地图时所做的那样)。

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. OpenLayers使用基于纬度和经度的平面坐标系(EPSG:4326),而Google Maps,Microsoft Virtual Earth,Yahoo Maps和其他商业API提供者使用也称为Spherical Mercator Projection的球坐标系(EPSG:3857)。 See these related SO questions here and here . 在此处此处查看这些相关的SO问题。

Fortunately OpenLayers has APIs that let you transform your coordinates between various projections so that all of your layers play nicely together. 幸运的是,OpenLayers拥有API,可让您在各种投影之间转换坐标,以便所有图层都可以很好地协同工作。 The OpenLayers documentation dedicated a whole chapter to 'Spherical Mercator Projection' which contains some examples. OpenLayers文档将整章专门介绍了“球形墨卡托投影” ,其中包含一些示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM