How to get the distance in a near func

In the




Syntax Example: near(predicate, [long, lat], distance)

How to get the actual distance from the result to the specified coordinates [long, lat]?
I want the results to be sorted by distance

Do you say a straight line? this is not possible in Dgraph. Your map lib/framework just needs the points and then it decides which route it gives you if it needs to generate a route with distance. And straight lines are not so useful. Even for helicopters :stuck_out_tongue:

There is also no sorting for the Geo type. Even if there was a way (it could be introduced) to tell the distance between two points, there would be no way to do sorting.

If we see more interest on this topic, we can make it a feature request in the future.

I get it
I thought that since the dgraph knows which points is NEAR to the point I gave,it must have calculated their distance in the backend already and I can get those value easily.

thanks for your explanation :smile:

1 Like

It does, but that calculations aren’t exposed, because they are not useful at first. And also it is necessary to create a support for geo in sorting. So that would be a feature request.

1 Like

I thought I saw somewhere already a request for geo sorting, idk.

I would find this useful and if it not available in the back-end, i will be doing it on the front-end. I don’t need exact distances but just a general idea as the bird flies. I want to give my users a rough idea how far apart two a set of locations are from the source.

If I am in city at lat,lng then is this location approximately 5 miles away or 500 miles away.

1 Like

I could use this feature too. My use case is that for people who want to find events near them, they should be able to see the events sorted by approximate distance. Right now, my react app is fetching the events, taking their latitude and longitude, and calculating the distance between the points in order to sort the results. That works fine for now, but eventually there could be a problem because the UI can only sort one page of results by distance at a time. So if I want to get events within a time range and then sort them by distance, some events would be counterintuitively excluded from the first page of results because I wouldn’t have a good way to prioritize the distance over the time.

I came to this question because I was trying to figure out why Dgraph thought a certain point was more than 100 km away when in reality it’s less than 60km. Without the distance, I don’t know how far away Dgraph thinks the point is, so it’s also a bit harder to troubleshoot this. Edit: I didn’t read the docs. The distance is in meters, not km.