Dumarest Dumarest - 16 days ago 6
Javascript Question

How to round down google map coordinates in my getElementById.innerHTML

I'm using google maps and displaying map coordinates when the map is clicked:



google.maps.event.addListener(map, 'click', function (getcoords) {
document.getElementById('bar').innerHTML = (getcoords.latLng.lat() + "," + getcoords.latLng.lng());
});


The coordinates returned have atomic scale precision like so: 55.8907220038651,-2.3968541622161865

How do round these down to five decimal places?

I thought of using toFixed(5), but don't know how to implement this.

mx0 mx0
Answer

Use .toFixed() on .lat() like this

google.maps.event.addListener(map, 'click', function (getcoords) {
    document.getElementById('bar').innerHTML = (getcoords.latLng.lat().toFixed(5) + "," + getcoords.latLng.lng().toFixed(5));
});
Comments