Below is my map script, everything is working pretty fantastic! However, when I click and it sends the e.latlng to the form it is sending

LatLng(38.47939, -105.86426) for example. I would prefer that it only send 38.47939, -105.86426.

How can I remove the text and parentheses?

var mymap = L.map('mapid').setView([38.47939, -99.49219], 5);

L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/streets-v10/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoiYnJldHQyMjI3OCIsImEiOiJjajAzNzZjZTkwOWNtMzJxbHZjMmVwZ2hyIn0.cWzkt6Gj_N3SZ8GKzqhByA', {

    var popup = L.popup();

    function onMapClick(e) {
    document.getElementById('lat-lng').value = e.latlng.toString();
            .setContent("Copy and Paste into the Form \n" + e.latlng.toString())


    mymap.on('click', onMapClick);


If you console.log(e.latlng) and observe the output, you will see that e.latlng is an object with two properties: { lat, lng }. Thus we can output latitude and longitude independently and concatenate them to a string:

e.latlng.lat + ', ' + e.latlng.lng // '37.75334401310659, -107.09472656250001'

Using ES6 template literals (doesn't work in Internet Explorer) you could do the following:

`${e.latlng.lat}, ${e.latlng.lng}` // slightly more readable

you'll need to save e.latlng.toString to a new variable and clean up the data from there. something like:

var myLL=e.latlng.toString();
var myC=myLL.substring(7,myLL.length-1).split(',');
return myC[0].trim + myC[1].trim;

there may very well be a simpler builtin way to do this...

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.