0

I'm new to this forum, hope to get support about my question on Leaflet. I'm using it as part of a web that I'm using and I would like to reduce the number of "decimals" that I getting automatically from the source.

I've tried already the .toFixed() function but it seems not recognized at all. Since this number is too extended I notice the leaflet will not build the map as I want. For that reason, I would like to reduce this number to the maximum capacity that the leaflet will permit me.

My actual code give values like:

40,4560796748558, -3,5703805143365

While the function lang() returns the error below:

Uncaught Error: Invalid LatLng object: (40,4560796748558, -3,5703805143365)

Thanks in advance.

ocrdu
  • 2,172
  • 6
  • 15
  • 22

3 Answers3

1

This sounds less like a leaflet problem and more like a math problem. Using .toFixed transforms a number to a string. You can do Number(lat.toFixed(decimalPlaces)) to round a number without transorming it into a string. Or check out this answer on how to round to x decimal places in javascript. L.latLng expects two number values, not two string values, hence the error.

Seth Lutske
  • 9,154
  • 5
  • 29
  • 78
  • Thanks, i see... but also if i try to convert to a num using the toFixed() function i got an error saying this: Uncaught TypeError: latn.toFixed is not a function – Fausto Palacios Feb 19 '21 at 17:19
0

Thanks to Seth, i find out the solutions, basically i have had to reduce the number on digit to parse to latLng function in LeafLet, so i did what i have bellow:

    const lngn = nav.lng ;
    const lngnp= lngn.replace(",", ".");
    const lng  = lngnp.replace(/\d+\.\d+/g, function(match) {
            return + (Math.round(match+ "e+8")  + "e-8");
            });
    points.push(L.latLng(lat, lng));

my source number has been parsed with the "," so i have had to replace it first with a "." as separator and then i did the replace function to get the final number of digit that i want.

Thanks to All.

0
function setPrecision(lat, lng, prec = 7) {
  return {
    lat: Number(lat.toPrecision(prec)),
    lng: Number(lng.toPrecision(prec))
  }
}
stefcud
  • 2,220
  • 4
  • 27
  • 37