manuelbieh / geolib

Zero dependency library to provide some basic geo functions
MIT License
4.21k stars 341 forks source link

Cannot read property 'latitude' of undefined #82

Closed kursata closed 5 years ago

kursata commented 8 years ago

Hi

We're experiencing following error while calling findNearest() function. The error occurs in some of the calls. The inputs parameters of successful and unsuccessful calls are identical. Any idea on the cause of the error or a fix on handling the type error will be appreciated.

TypeError: Cannot read property 'latitude' of undefined at Geolib.geolib.extend.coords (/var/app/current/node_modules/geolib/dist/geolib.js:179:121) at Geolib.geolib.extend.getDistance (/var/app/current/node_modules/geolib/dist/geolib.js:248:26) at Geolib.geolib.extend.orderByDistance (/var/app/current/node_modules/geolib/dist/geolib.js:924:37) at Geolib.geolib.extend.findNearest (/var/app/current/node_modules/geolib/dist/geolib.js:1013:32) at /var/app/current/BAL/Content/Watchlist/Watchlist.js:633:56 at fn (/var/app/current/node_modules/async/lib/async.js:746:34) at /var/app/current/node_modules/async/lib/async.js:1213:16 at /var/app/current/node_modules/async/lib/async.js:166:37 at /var/app/current/node_modules/async/lib/async.js:706:43 at /var/app/current/node_modules/async/lib/async.js:167:37 at Immediate._onImmediate (/var/app/current/node_modules/async/lib/async.js:1206:34) at processImmediate as _immediateCallback

lprhodes commented 7 years ago

Seeing the exact same issue.

manuelbieh commented 7 years ago

Can you provide some code examples maybe?

lprhodes commented 7 years ago

I'm debugging the issue at the moment - I'll update shortly.

lprhodes commented 7 years ago

@manuelbieh It occurs when coordinates with additional precision are provided.

e.g. { latitude: 40.7536853999999, longitude: -73.9991637 }

nitindhar7 commented 7 years ago

@manuelbieh might it be related to this #113 ?

neilakoh commented 7 years ago

I am also having this issue right now but only on IOS. . it works fine on android . . in my case I added onPress event on the map to get coordinates. Here is my code:

https://github.com/airbnb/react-native-maps/issues/1499

It appears that on IOS the callback is missing some data or it did not return properly. Please advise. Thank you.

neilakoh commented 7 years ago

any update on this?

ianawilson commented 7 years ago

I don't know if this is the same issues others are seeing, but I also ran into it like this:

This (and #130) can both be caused by using the navigator.geolocationAPIs with geolib. The Coordinates object returned by a call to getCurrentPosition() or watchPosition() doesn't hasOwnProperty() for either 'latitude' or 'longitude', although the implementing object can retrieve values for these properties.

This means that coords(), delegating to getKeys()'s, fails to find latitude and longitude properties, and returns undefined. Other functions that depend on coords() (most of them, I believe), throw this error because they assume that coords() will return a valid object, but it does not, resulting in this error.

kirakik commented 7 years ago

@ianawilson did you figure out how to fix this?

ianawilson commented 7 years ago

@kirakik My workaround was to map the Coordinates object to a POJO, eg,

navigator.geolocation.getCurrentPosition(function(position) {
  // or whatever geolib function
  geolib.longitude({
    latitude: position.coords.latitude,
    longitude: position.coords.longitude,
  });
});

or map when I had a collection of positions,

geolib.getPathLength(positions.map((p) => ({
  latitude: p.coords.latitude,
  longitude: p.coords.longitude,
})));

But I would love to see geolib work nicer with the geolocation API!

manuelbieh commented 5 years ago

I was pretty sure that already worked 🤔 Should be fixed again in v3.0.0 which I will release shortly.