Hi Andreas,<div><br></div><div>Thanks a lot for the solution using callbacks. I was wondering if you could suggest how that might be adapted to give the measurements on a draw feature. It's much easier than what I was previously doing for measure and drawFeature (passing a customized modifyFeature function in the options, where the new function was the same as the original, but also had the requisite getLength statements - functional, but it seems like this should be far more version dependent than the callback solution).</div>
<div><br></div><div>When I try to use callbacks-modify on a drawFeature (as below), my modify callback doesn't seem to get used (the console.log(this) never fires). I assume this is because measureControls didn't already use a modify callback, whereas drawFeature does, and as such drawFeature overrides any modify callback I might try to pass in? Is there any way to append to the normal modify callback? Am I missing something obvious?</div>
<div><br></div><div>Thanks! </div><div><br></div><div>Josh</div><div><br></div><div>broken: uses callbacks - modify: </div><div><div><span class="Apple-style-span" style="font-style: italic;"> selectDrawCircle = new OpenLayers.Control.DrawFeature(selectLayerPoly,</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> OpenLayers.Handler.RegularPolygon,{</span></div><div><span class="Apple-style-span" style="font-style: italic;"> displayClass:"olControlSelectDrawCircle",</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> element:document.getElementById('SelectControl_Distance'),</span></div><div><span class="Apple-style-span" style="font-style: italic;"> unitElement:document.getElementById('SelectControl_mapUnit'),</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> callbacks:{</span></div><div><span class="Apple-style-span" style="font-style: italic;"> modify: function (point,feature) {</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> console.log(this);</span></div><div><span class="Apple-style-span" style="font-style: italic;"> this.element.value=this.radius.toFixed(2);</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> this.unitElement.innerHTML=this.map.getUnits(); </span></div><div><span class="Apple-style-span" style="font-style: italic;"> }</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> },</span></div><div><span class="Apple-style-span" style="font-style: italic;"> handlerOptions:{</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> sides:40</span></div><div><span class="Apple-style-span" style="font-style: italic;"> }</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> });</span></div><div><span class="Apple-style-span" style="font-style: italic;"> <span class="Apple-style-span" style="font-style: normal; "></span></span></div>
<div><span class="Apple-style-span" style="font-style: italic;"><span class="Apple-style-span" style="font-style: normal; ">functional: uses slightly customized modifyGeometry function passed in as an option</span></span></div>
<div><div><span class="Apple-style-span" style="font-style: italic;"> selectDrawCircle = new OpenLayers.Control.DrawFeature(selectLayerPoly,</span></div><div><span class="Apple-style-span" style="font-style: italic;"> OpenLayers.Handler.RegularPolygon,{</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> displayClass:"olControlSelectDrawCircle",</span></div><div><span class="Apple-style-span" style="font-style: italic;"> handlerOptions:{</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> sides:40,</span></div><div><span class="Apple-style-span" style="font-style: italic;"> element:document.getElementById('SelectControl_Distance'),</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> unitElement:document.getElementById('SelectControl_mapUnit'),</span></div><div><span class="Apple-style-span" style="font-style: italic;"> modifyGeometry: function() {</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> var angle, dx, dy, point;</span></div><div><span class="Apple-style-span" style="font-style: italic;"> var ring = this.feature.geometry.components[0];</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> // if the number of sides ever changes, create a new geometry</span></div><div><span class="Apple-style-span" style="font-style: italic;"> if(ring.components.length != (this.sides + 1)) {</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> this.createGeometry();</span></div><div><span class="Apple-style-span" style="font-style: italic;"> ring = this.feature.geometry.components[0];</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> }</span></div><div><span class="Apple-style-span" style="font-style: italic;"> for(var i=0; i<this.sides; ++i) {</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> point = ring.components[i];</span></div><div><span class="Apple-style-span" style="font-style: italic;"> angle = this.angle + (i * 2 * Math.PI / this.sides);</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> point.x = this.origin.x + (this.radius * Math.cos(angle));</span></div><div><span class="Apple-style-span" style="font-style: italic;"> point.y = this.origin.y + (this.radius * Math.sin(angle));</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> point.clearBounds();</span></div><div><span class="Apple-style-span" style="font-style: italic;"> }</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> // note, only val b/c this is an input field.</span></div><div><span class="Apple-style-span" style="font-style: italic;"> this.element.value=this.radius.toFixed(2);</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> this.unitElement.innerHTML=this.map.getUnits();</span></div><div><span class="Apple-style-span" style="font-style: italic;"> }</span></div>
<div><span class="Apple-style-span" style="font-style: italic;"> }});</span></div></div></div><div><div><br></div><br><div class="gmail_quote">On Wed, Apr 22, 2009 at 8:22 AM, Andreas Hocevar <span dir="ltr"><<a href="mailto:ahocevar@opengeo.org">ahocevar@opengeo.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi,<br>
<br>
the solution (if you're using trunk or 2.8) is quite simple. The snippet<br>
below replaces the definition of measureControls in the measure example<br>
(starting around L65)<br>
<br>
measureControls = {<br>
line: new OpenLayers.Control.Measure(<br>
OpenLayers.Handler.Path, {<br>
callbacks: {<br>
modify: function(point, feature) {<br>
measureControls.line.measurePartial(point,<br>
feature.geometry);<br>
}<br>
},<br>
persist: true,<br>
handlerOptions: {<br>
layerOptions: {styleMap: styleMap}<br>
}<br>
}<br>
),<br>
polygon: new OpenLayers.Control.Measure(<br>
OpenLayers.Handler.Polygon, {<br>
callbacks: {<br>
modify: function(point, feature) {<br>
<br>
measureControls.polygon.measurePartial(point,<br>
feature.geometry);<br>
}<br>
},<br>
persist: true,<br>
handlerOptions: {<br>
layerOptions: {styleMap: styleMap}<br>
}<br>
}<br>
)<br>
};<br>
<br>
As you can see, the trick is to add a modify callback to the handler,<br>
calling the control's measurePartial method.<br>
<br>
Regards,<br>
Andreas.<br>
<div><div></div><div class="h5"><br>
pribram pribram wrote:<br>
> Hi,<br>
> Is there anything new about measuring + getting distance on mousemove?<br>
> <a href="http://n2.nabble.com/Measure-Control---get-distance-on-mouse-move-td2503580.html" target="_blank">http://n2.nabble.com/Measure-Control---get-distance-on-mouse-move-td2503580.html</a><br>
> If there is a solution, please publish it.<br>
><br>
> Thanks,<br>
> Pribram<br>
> _______________________________________________<br>
> Users mailing list<br>
> <a href="mailto:Users@openlayers.org">Users@openlayers.org</a><br>
> <a href="http://openlayers.org/mailman/listinfo/users" target="_blank">http://openlayers.org/mailman/listinfo/users</a><br>
><br>
<br>
<br>
</div></div><font color="#888888">--<br>
Andreas Hocevar<br>
OpenGeo - <a href="http://opengeo.org/" target="_blank">http://opengeo.org/</a><br>
Expert service straight from the developers.<br>
</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@openlayers.org">Users@openlayers.org</a><br>
<a href="http://openlayers.org/mailman/listinfo/users" target="_blank">http://openlayers.org/mailman/listinfo/users</a><br>
</div></div></blockquote></div><br></div>