Reputation: 93
Ran into a small problem when change to OpenLayers 2.11 with showing the length of each segment on the measuring line.
Previously after placing points A --> B, B --> C. The length of segment A to B would show up between those points, likewise with B to C and after ending the line the total would be displayed.
My script worked fine with OpenLayers 2.10, but after changing to OpenLayers 2.11 as soon as I end the line only the segment values disappear from the vector layer.
I understand the problem, but am having trouble working out the logic...
Below is the script I am using:
line: new OpenLayers.Control.Measure(OpenLayers.Handler.Path, {
persist: true,
handlerOptions: {
layerOptions: {
renderers: renderer,
styleMap: styleMap
}
},
textNodes: null,
callbacks:{
create:
function(){
this.textNodes = [];
vlayer.destroyFeatures(vlayer.features); /*-- figured this area is the problem */
mouseMovements = 0;
},
modify:
function(point, line){
if(mouseMovements++ < 5){
return;
}
var len = line.geometry.components.length;
var from = line.geometry.components[len -2];
var to = line.geometry.components[len -1];
var ls = new OpenLayers.Geometry.LineString([from, to]);
var dist = this.getBestLength(ls);
if(!dist[0]){
return;
}
var total = this.getBestLength(line.geometry);
var label = dist[0].toFixed(3) + " " + dist[1];
var textNode = this.textNodes[len -2] || null;
if(textNode && !textNode.layer){
this.textNodes.pop();
textNode = null;
}
if(!textNode){
var c = ls.getCentroid();
textNode = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(c.x, c.y), {}, {
label: "",
fontColor: "#800517",
fontSize: "12px",
fontFamily: "Tahoma",
fontWeight: "bold",
labelAlign: "cm"
});
this.textNodes.push(textNode);
vlayer.addFeatures([textNode]);
}
textNode.geometry.x = (from.x + to.x) / 2;
textNode.geometry.y = (from.y + to.y) / 2;
textNode.style.label = label;
textNode.layer.drawFeature(textNode);
this.events.triggerEvent("measuredynamic", {
measure: dist[0],
total: total[0],
units: dist[1],
order: 1,
geometry: ls
});
}
}
2011-11-28
After researching a bit, I've come to the conclusion that in OpenLayers 2.11 the measure partial control is activated as soon as the button is clicked. Previously in OpenLayers 2.10 the measure partial control was activated when the map was clicked and ended at the same time the path was ended, so the reason it worked before was that the textnodes array would be empty only at the start of a new line, but in 2.11 the measure partial control does not deactivate once at the end of the path, so in effect reverting back to an empty array and erasing the layer features when I end the line.
Source: http://trac.osgeo.org/openlayers/ticket/3315
Could anyone elaborate a bit more about this?
Upvotes: 0
Views: 1800
Reputation: 93
Just in case anyone wants to know the answer. https://github.com/jorix/OL-DynamicMeasure
Upvotes: 1