Reputation: 27
I have a very odd problem with javascript. I made a map with OpenLayers and mapdata from OSM.
Everytime I draw a feature (point or line) I fire a function when the sketch is completed called redrawFeatures
mainly to split the lines and to give every line a start and an endpoint. Everything works fine so far, but I also want to transform the drawn points (and lines) form the mapprojection (EPSG:900913) to the displayprojection (EPSG:4326) and there is the problem. The following piece of code is the function:
var pointsOnMap = [];
var linesOnMap = [];
function redrawFeatures(e) {
var vert = e.feature.geometry.getVertices();
var points = [];
var pointFeatures = [];
var lineFeatures = [];
for (var i = 0; i < vert.length; i++) {
var point = new OpenLayers.Geometry.Point(vert[i].x, vert[i].y);
points.push(point);
alert(points[i].x + ', ' + points[i].y);
pointFeatures.push(new OpenLayers.Feature.Vector(point));
alert(points[i].x + ', ' + points[i].y);
pointsOnMap.push(point.transform(new OpenLayers.Projection("EPSG:900913"),
new OpenLayers.Projection("EPSG:4326")));
alert(points[i].x + ', ' + points[i].y);
if (i >= 1) {
var line = new OpenLayers.Geometry.LineString([points[i-1], points[i]]);
lineFeatures.push(new OpenLayers.Feature.Vector(line));
linesOnMap.push(line.transform(new OpenLayers.Projection("EPSG:900913"),
new OpenLayers.Projection("EPSG:4326")));
}
}
draw.destroyFeatures([e.feature]);
draw.addFeatures(lineFeatures);
draw.addFeatures(pointFeatures);
}
The variable named draw
is the layer I draw on.
I determine the coordinates from the event, create a new point (line 11) and save them to an array (line 13). Turn the same point into a feature and save it to another array, as well (line 15) The Problem is, when I later transform this point to the displayprojection and save this point in a third array (line 17) the coordinates are also changed in the previously saved value of the first array called points
. I alert the values of the saved coordinates trice and this is what I get and don't understand:
Alert 1 (line 14) That's right I get the coordinates of the point
Alert 2 (line 16) That's still right (Same as the first one)
Alert 3 (line 19) Now it turns wrong! I alert the same variable like before but after the transformation of point
the value of points[i]
also changed without any reason.
Is there any mistake I don't realize?
Upvotes: 1
Views: 147
Reputation: 2352
It seems that the transform function operates in-place, and modifies the existing point
data. You should clone the point and then operate on that instead if you want to preserve the previous value
clone = point.clone();
Upvotes: 2
Reputation: 8041
In javascript:
var a = [1,2,3]
var b = a; //Here you are actually copying the reference of a
b.push(4); //This will also add 4 to a...a = [1,2,3,4] now
//In order to actually copy the array instead of reference u need to clone it as follows
var c = a.slice(); //creates a clone of a
c.push(5); // c = [1,2,3,4,5] and a = [1,2,3,4]
Upvotes: 1