Array.map is not working with negative coords (latitude)

I have a Data array with Points. When I calculate the distance from My location to those Points (Data.map) as long as the Points have positive latitude it works. As soon as I change the latitude to negative it stops working.

 Data: [{id: '1', Point: '-34.909021, -56.220080', A: '3',},
               {id: '2', Point: '-34.934356, -56.160794', A: '4',},
              {id: '3', Point: '-34.910569, -56.132107', A: '6',}],

    const distances = this.state.Data.map(
      (item) =>
        !!(coords.longitude && coords.latitude) &&
        ({ distance: getPreciseDistance(coords, item.Point), A: item.A})
    )
      .filter(Boolean)
      .sort()
      .map((item) => <Text>{item.distance}, {item.A}</Text>);

What’s this function? What does this do?

It calculates the distance between my coords.latitude & coords.longitude and the Points in the Data array. It’s a from the geolib library, alone by itself it works fine but the problem appears when I use map.

What wrong output it gives?

If I use positive latitude in Points it works fine.
But when I change the Points latitude to negative it doesn´t return anything (if I toggle the Boolean filter it returns NaN)

I am not sure you are giving it the input it wants

GitHub - manuelbieh/geolib: Zero dependency library to provide some basic geo functions

I replaced point B in the function with my lat & my long and it works fine calculating the distance to a point A.
The problem is when I try to map an array of Points.

Doesn’t it want an object as input?

With Data: [{id: '1', Point: '34.909021, -56.220080', A: '3',},...... it accepts the object.
But when I change the sign from 34 to -34 in Data: [{id: '1', Point: '-34.909021, -56.220080', A: '3',},...... the error appears.
I think it must have something to do with this line !!(coords.longitude && coords.latitude) &&, I suppose the double !! has something to do, but if I take it out NaN.

!! converts a value to it’s boolean equivalent.

So it’s not the problem…

what happens if you give it an object with latitude and longitude properties, but they are negative?

NaN. I tried testing diferences, if the longitude in Points array is negative it works. If the LATITUDE in the Points array is negative I get NaN.

I was looking insite the repo, it expects objects in the form { latitude: 37.774514, longitude: -122.418079 }

this is the file

I don’t understand what this code is trying to do.

This says

  1. Take an element from the Data array (bad variable name)

  2. Determine if both coords.longitude and coords.latitude are truthy and convert this to a boolean value

  3. Make an object out of the distance between coords and the current item and return this object if the coords lat and long are truthy?

  4. Filter this array, which basically emptying this array if coords lat/long is not truthy

  5. Sort this array

  6. Map this array yet again

Is this correct? I feel like this is unnecessarily complex

1 Like

Yes you’re right Jeremy. The idea is to calculate the distance from MyLocation to all the markers in “Data array” (map it) and pick out the nearest one.
I agree it´s not clear

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.