移动OpenLayers.Feature.Vector或OpenLayers.Geometry.Point失败



注意:这与:如何以编程方式移动OpenLayers矢量不一样?

我有一个简单的openlayers映射项目。我需要在上面显示和移动一些矢量

我创建了这样的矢量,效果很好:

var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point( unit.lon,unit.lat ).transform(epsg4326, projectTo),
                            {description:'This is the value of<br>the description attribute'} ,
                            {externalGraphic: '/assets/admin/layout/img/avatar/' + unit.id + '.png', graphicHeight: 74, graphicWidth: 60, graphicXOffset:-12, graphicYOffset:-25  }
                    );
feature.id = unit.id;
vectorLayer.addFeatures(feature);

然而,我正试图将这些向量移动到一些精确的LonLat。我尝试了很多东西。其中之一如下:

 var feature = vectorLayer.getFeatureById(unit.id);
 movePoint(feature.point, unit.lon, unit.lat);        
vectorLayer.redraw();
function movePoint(point, x, y) { point.x = x; point.y = y; point.clearBounds(); }

另一个是:

var feature = vectorLayer.getFeatureById(unit.id);
feature.geometry.move(unit.lon, unit.lat);
vectorLayer.redraw();

据我所知,最后一个移动方法使用像素差异。但是我不想使用差分。相反,直接使用精确的经度和纬度参数。

那么再一次,用编程的方式将向量/点移动到一个确切的位置是什么?

我在我的项目上有谷歌地图和OSM,投影问题会是一个问题吗?

我刚刚开始开发openlayers。

我相信,用于创建矢量特征的点被分配给该特征的几何属性。

尝试在第一个示例中设置feature.geometry.x和feature.geometry.y,而不是设置feature.point.

用小提琴更新,主要部分是:

    var targetLoc = new OpenLayers.LonLat(-16, 50).transform(epsg4326, projectTo);
    feature.geometry.x = targetLoc.lon;
    feature.geometry.y = targetLoc.lat;
    vectorLayer.redraw();

这很可能与投影有关。如果你使用谷歌地图和OSM,那么你的坐标系统是3857(球面墨卡托),单位是米。您需要首先将您的纬度转换为这个,然后调用move,例如,

var fromProj = new OpenLayers.Projection("EPSG:4326");
var toProj = new OpenLayers.Projection("EPSG:3857");
var point = new OpenLayers.LonLat(-1, 52);
point.transform(proj, toProj);
feature.geometry.move(point);

文档中有一些有用的信息。

你也可以在地图构造函数中设置mapProjection和displayProjection,然后你可以使用:

point.transform(fromProj, map.getProjectionObject()) as well.

关于设置地图投影属性的更多信息,请参见此gis.stackexchange.com答案

最新更新