[OpenLayers-Users] Drag+Line=Distance in KMs

Steve Nelson snelson at webapper.com
Sat Apr 21 01:11:49 EDT 2012


Dragging is a tiny bit more complex, but the logic is essentially the same.
This is totally untested, but maybe it'll get you going in the right
direction. I added some inline comments. Give it a whirl.


var firstClickMercator;
OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {

defaultHandlerOptions: {
'single': true,
'double': false,
'pixelTolerance': 0,
'stopSingle': false,
'stopDouble': false
},
handleRightClicks:true,
initialize: function(options) {
this.handlerOptions = OpenLayers.Util.extend(
{}, this.defaultHandlerOptions
);
OpenLayers.Control.prototype.initialize.apply(
this, arguments
);
this.handler = new OpenLayers.Handler.Click(
this, {
'click': this.singleClick
}, this.handlerOptions
);
},
 singleClick: function(e) {
  //You want to convert the xy coordinate in the e variable to a LonLat
var clickLonLat=map.getLonLatFromPixel(e.xy)
 //Projections confuse the hell out of me. But what I've learned is you
probably want to transform to a Mercator projection
var clickLonLatMercator=clickLonLat.transform(map.getProjectionObject(),new
OpenLayers.Projection("EPSG:4326"));
 if (!firstClick){
 //If this is the first store it into a global variable:
firstClickMercator=clickLonLatMercator
}else{
 //if the global variable firstClick already exists, calculate the distance
in KM
var
distanceInKM=OpenLayers.Util.distVincenty(firstClickMercator,clickLonLatMercator)
 //you'll have to output it where you want, this should display the value
in the google chrome console:
console.log(distanceInKM)
 //reset the global variable to null.
firstClick=null;
}
},

});


//Define the global map:
var map = new OpenLayers.Map("map")

//Add the click control to the map
var click = new OpenLayers.Control.Click();
map.addControl(click);
click.activate();



On Fri, Apr 20, 2012 at 10:41 AM, alexe100 <imoveisnacionais at gmail.com>wrote:

> Hi all , i am new here and in GEO* purposes...
> I am doing a web based application where i need to show some markers, that
> it aldready working.
> Now I need to give the oortunity to the user of drag a line on the map and
> while dragging see the distance in kms in a label box (html external obj)
> I saw already the openlayer example for distance but it has various options
> (and that make me confusing because i am a beginner). I did not found a
> (very) simple example doing this.
> Please, could you tell me how to do this. A littl example would be very
> appreciated.
>
> Thanks a lot
>
> Alex
>
> --
> View this message in context:
> http://osgeo-org.1560.n6.nabble.com/Drag-Line-Distance-in-KMs-tp4902194p4902194.html
> Sent from the OpenLayers Users mailing list archive at Nabble.com.
> _______________________________________________
> Users mailing list
> Users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120421/6252f2ee/attachment.html


More information about the Users mailing list