簡體   English   中英

移動OpenLayers.Feature.Vector或OpenLayers.Geometry.Point失敗

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM