manuelbieh / geolib

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

Documentation for 'findNearest' is misleading #113

Closed nitindhar7 closed 5 years ago

nitindhar7 commented 7 years ago

I just lost some time trying to debug this message:

undefined is not an object (evaluating 'this.getKeys(point).latitude')

The data I was passing in for the coords argument to the findNearest function looks like this:

{
    "6f94619c-c17c-11e6-a4a6-cec0c932ce01": {
        latitude: -73,
        longitude: 42
    },
    ...
}

From the examples, the structure of coords looks like:

var spots = {
    "Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
    "Dortmund U-Tower": {latitude: 51.515, longitude: 7.453619},
    ...
}
// in this case set offset to 1 otherwise the nearest point will always be your reference point
geolib.findNearest(spots['Dortmund U-Tower'], spots, 1)

or generally speaking:

{
    name: {latitude: ..., longitude: ...}
}

The code doesn't seem to be expecting a top level string key (like "Brandenburg Gate, Berlin" in the readme example):

/**
        * Finds the nearest coordinate to a reference coordinate
        *
        * @param        object      reference coordinate e.g. {latitude: 51.5023, longitude: 7.3815}
        * @param        mixed       array or object with coords [{latitude: 51.5143, longitude: 7.4138}, {latitude: 123, longitude: 123}, ...]
        * @return       array       ordered array
        */
        findNearest: function(latlng, coords, offset, limit) {
        ...
LoriKarikari commented 7 years ago

nitindhar7 I had this issue too when is was using an array.

[
    "Aalborg Airport": {latitude: '57.0952', longitude: '9.85606'},
    "Newport News": {latitude: '36.9805', longitude: '-76.4295'},
    ...
]

I converted it to an object and now I'm not getting that error anymore.

manuelbieh commented 5 years ago

This function was massively simplified in v3 which will be released shortly.