简体   繁体   中英

move OpenLayers.Feature.Vector or OpenLayers.Geometry.Point fails

Note: This is not same with: How to move OpenLayers Vector programmatically?

I have a simple openlayers map project. I need to show and move some Vectors on of it.

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. 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?

I just started to develop 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.

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. You will need to convert you lat/lon to this first and then call move, eg,

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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