简体   繁体   中英

How to show map mouse hover values on text box using openlayers3?

I am working on ol3, and I have the following mouse hover code, which gets latitudes and longitudes, then throws them on the "DIV" whose id is 'mouse-position':

var mousePositionControl = new ol.control.MousePosition({
  coordinateFormat: ol.coordinate.createStringXY(4),
  projection: 'EPSG:4326',
  // comment the following two lines to have the mouse position
  // be placed within the map.
  className: 'custom-mouse-position',
  target: document.getElementById('mouse-position'),
  undefinedHTML: ' '
});

and the corresponding html div is:

<div class="span6" id="mouse-position">&nbsp;</div>

PROBLEM: I want to show these latitude/longitude values to be shown on the "TEXTBOX" , rather than to simply show them on a div.

I simple changed the div id to textbox's id in "target" field, but it didn't work! I also played around with value attribute but couldn't find the solution. I am looking forward for the suggestions, thank you very much for the time. :)

NOTE: The complete code of these above snippets can be found at this link: http://openlayers.org/en/master/examples/mouse-position.html

If by "textbox" you mean textarea then this won't work. Textarea requires a value rather than a "innerHTML like".

try this to get coordinates:

var corde = document.getElementsByClassName("custom-mouse-position");
var resultElement=corde[0].innerHTML

then set this value to your textbox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM