-1

I would like to add the coordinates as a click to the map as a form entry, I do not know how the function structure should look like

map.on('click', function(e) {
    var lat = e.latlng.lat;
});

<input type="text" name="coordy" value=" <script type="text/javascript">  document.write(lat) </script> ">

It is not working.

5

Create your <input> with an unique id, so then later you can get a reference to it via document.getElementById():

<input type="text" id="coordy" />

There is no need to include a value attribute in this <input>, as the value will be overwritten later. See the documentation for the <input> HTML tag about its value attribute. Note that the value attribute is not javascript.

In your Leaflet event handler: Get a reference to the HTMLInputElement that corresponds to your <input> HTML tag, and then set the value property of that HTMLInputElement.

map.on('click', function(e) {
  var lat = e.latlng.lat;
  document.getElementById('coordy').value = lat;
});

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.