Skip to content

Added near by search support #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gagan-bansal
Copy link

I have added near by search support. It works based on geohash concept. Redis also uses same concept for near search.

Query syntax:

GET /cities?near=lat,lon&distance=meters

There is no index created in json-server at run time for near search query.
On latitude and longitude property of data geohash is calculated and stored along data. The geohash is used for nearby search. I have used node-geohash and geo-nearby modules to achieve the same.

Sample output

{
  "id": 1,
  "city": "Jeogiwe",
  "lat": -28.12785,
  "lon": -145.96895,
  "geohash": 344516337195371
}

To generate the sample random location data use following script

var dream = require('dreamjs')
var hash = require('ngeohash')
var jsonfile = require('jsonfile')
dream.customType('incrementalId', function(helper){
      return helper.previousItem ? helper.previousItem.id+1 : 1;
})
var data = dream
.schema({
  id: 'incrementalId',
  city: 'city',
  lat: 'latitude',
  lon: 'longitude'
})
.generateRnd(10000)
.output()

data.forEach(function(city) {
  city.geohash = hash.encode_int(city.lat, city.lon);
})
jsonfile.writeFileSync('random-locations.json', {cities: data}, {spaces: 2});

One doubt from my side, I have added default latitude, longitude and geohash property name in src/server/router/index.js but there should be a better place to add default and overwrite these properties while creating an instance of json-server when used as module. At command line i have added option to override the default property.

@gagan-bansal
Copy link
Author

@typicode any feedback on this?

@typicode
Copy link
Owner

typicode commented Apr 20, 2016

Hi @gagan-bansal,

Sorry about that. Thank you for the very good PR.
I'm a little busy right now with hotel but I'll check this as soon as I can.

@gagan-bansal
Copy link
Author

@typicode thanks, that is fine.

@gagan-bansal
Copy link
Author

@typicode I saw this today my old pull request.
Is it helpful now, should I look into this again to resolve the conflicts?

If you think this feature is useful I can look again.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants