[英]move OpenLayers.Feature.Vector or OpenLayers.Geometry.Point fails
注意:这与以下内容不同: 如何以编程方式移动OpenLayers Vector?
我有一个简单的openlayers地图项目。 我需要显示并移动一些Vector。
我创建这样的向量,并且效果很好:
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();
这很可能与投影有关。 如果您使用的是Google Maps和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答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.