I have a line feature in a vector layer "A", while I mouse over on that line a point should be added to vector layer "B" with current mouse position.


How can get lat lon of mouse position while mouse over on that line?


this code can help you solving your problem.

     var vectorLayer_B = new OpenLayers.Layer.Vector("Point Layer");
     var vectorLayer_A = new OpenLayers.Layer.Vector("Line Layer");

     var make_hover= function(e) {
       var coords = OpenLayers.Util.getElement("OpenLayers.Control.MousePosition").innerHTML;
       var xy = coords.split(',');
       var po = new OpenLayers.Geometry.Point(xy[0], xy[1]);
       var vc = new OpenLayers.Feature.Vector(po);

    var highlightCtrl = new OpenLayers.Control.SelectFeature(vectorLayer_A, {
       hover: true,
       highlightOnly: true,               
       eventListeners: {
            beforefeaturehighlighted: another_Function,
            featurehighlighted: make_hover,
            featureunhighlighted: another_Function


i hope it helps you...


If you want to add point to vector layer use OpenLayers.Controls.DrawFeatureExample. If you want to draw without map clicking try use OpenLayers.Events.getMousePosition can't say you more never use this.

If you want get mouse position use OpenLayers.Control.MousePosition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.