简体   繁体   English

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

[英]move OpenLayers.Feature.Vector or OpenLayers.Geometry.Point fails

Note: This is not same with: How to move OpenLayers Vector programmatically? 注意:这与以下内容不同: 如何以编程方式移动OpenLayers Vector?

I have a simple openlayers map project. 我有一个简单的openlayers地图项目。 I need to show and move some Vectors on of it. 我需要显示并移动一些Vector。

I create vectors like this and that works well: 我创建这样的向量,并且效果很好:

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);

However i'm trying to move these vectors to some exact LonLat. 但是,我试图将这些向量移动到确切的LonLat。 I've tried lots of things. 我已经尝试了很多东西。 One of them is below: 其中之一如下:

 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(); }

Other one is: 另一个是:

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

As i understood the last move method uses pixels differences. 据我了解,最后的移动方法使用像素差异。 But i dont want to use difference. 但是我不想使用差异。 Instead of that, use directly exact longitude and latitude parameters. 取而代之的是,直接使用精确的经度和纬度参数。

So again, what is the way to move vectors/points programmatically to an exact location? 那么,通过编程将向量/点移动到精确位置的方式又是什么?

I've google maps and also OSM on my project, could the projection issue be a problem? 我在项目中使用了谷歌地图和OSM,可能是投影问题吗?

I just started to develop openlayers. 我刚刚开始开发openlayers。 Any help would be greatly appreciated. 任何帮助将不胜感激。

I believe, the point used to create the vector feature is assigned to the geometry property of that feature. 我相信,用于创建矢量特征的点已分配给该特征的几何属性。

Try setting feature.geometry.x and feature.geometry.y in your first example instead of setting the feature.point. 尝试在第一个示例中设置feature.geometry.x和feature.geometry.y,而不要设置feature.point。

Updated with a fiddle , the main part being: 更新了小提琴 ,主要部分是:

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

It is very likely to be projection related. 这很可能与投影有关。 If you are using Google Maps and OSM then your coordinate system is 3857 (Spherical Mercator), which is in meters. 如果您使用的是Google Maps和OSM,则坐标系为3857(球形墨卡托),以米为单位。 You will need to convert you lat/lon to this first and then call move, eg, 您需要先将纬度/经度转换为此,然后再调用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);

There is some useful information in the docs . 文档中有一些有用的信息。

You can also set the mapProjection and displayProjection in the map constructor and then you can use: 您还可以在地图构造函数中设置mapProjection和displayProjection,然后可以使用:

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

See also this gis.stackexchange.com answer for some more info on settting projection properties of the map. 有关设置地图的投影属性的更多信息,另请参见gis.stackexchange.com答案

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

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