-
Notifications
You must be signed in to change notification settings - Fork 32
Ability to find nearby/close coordinate points #4
-
Hey,
I think it would be great if there was a feature that allowed to find close coordinates relative to the given point(s).
For instance, let's say you have the following records in the database:
--- id -------- name -------- latitude ---- longitude
1 Store A 20 30
2 Store B 25 35
3 Store C 22 37
4 Store D 26 31
5 Store E 29 38
And imagine the user location is at lat
of 19
and lng
of 30
and you want to show the the nearest store(s) relative to their location.
So with assumption of implemented feature we would have something like :
$coordinates = [ [ 20, 30], [ 25, 35], [ 22, 37], [ 26, 31], [ 29, 38], ]; // return only one single close coordinate: $closest = GeoFacade::getClosest([19, 30], $coordinates); // output : [20, 30] // return and sort all coordinates from closest to farthest : $closest = GeoFacade::getAllClosest([19, 30], $coordinates); // output : [ [20, 30], [ 26, 31], [ 22, 37], [ 25, 35], [ 29, 38] ]
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 2
Replies: 2 comments 4 replies
-
Great, this going to be in v2.1.0 after a few days.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
Nice! You already have a working algorithm for that ?
I've done some googling few months ago and I couldn't find anything on this topic, so I just gave up at the time.
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, I have, don't give up and wait for me to finish a new release, and I will explain how this feature solves a lot of works when you work with map providers.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
@HassanZahirnia
You can check the new release now! (v2.1.0)
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1
-
Hey Karam,
Thanks a lot for your hard work and thanks for adding the request feature 😄🙏
I just got time to try the getOrderByNearestNeighbor
function and it's working as expected 👍
But there are few points I wanted to mention:
- Currently with the way the
setPoints
is implemented, it's not possible to pass eloquent models or database rows that include alongitude
andlatitude
attribute/column, as thesetPoints
function only accepts an array of coordinates in format of[lat, lng]
. One may say extracting the coordinates from eloquent and passing it tosetPoints
is no problem and I agree with that, but the thing is, aftergetOrderByNearestNeighbor
returns the result, you'd have no idea which coordinate belongs to which model/record. So you'd have to write a loop includingwhere
clauses to trace back the coordinates to the relevant model - I'm getting a
Non static method 'setMainPoint' should not be called statically.intelephense(1036)
just wanted to put this out there. - I think
getOrderByNearestNeighbor
should be renamed togetNearestNeighbors
and maybe it could also take few arguments like :
getNearestNeighbors( $number_of_returned_neighbors, // this is to limit number of points returned $order_by = 'nearest', // this is to give ability to change order, maybe they want the farthest neighbors $include_main_point = false // this is to give option for whether to include the MainPoint in the results or not because currently it's included by default )
- Also this idea occurred to me, maybe the
NearestNeighbors
function could also take aradius
argument, to find points upto a certain radius (in km), because for example you may want to show the user the nearest restaurants and if there is no radius limitation, some restaurants stored in the database might be located in another city and you'd only want to show the user points that are located in their resident area.
I hope this was a useful feedback. Sorry If I'm all talk and not much of a contributor 😅 I'm way too of a noob to contribute to an open source project effectively
Beta Was this translation helpful? Give feedback.
All reactions
-
Hello Hassan and thanks for the comments,
1- In fact, if you look at my v2.2.0 pull request, you will find that I have already planned and I'm adding now what you are actually saying in relation to the order of the nearest neighbor algorithm in order to take the order from the farthest to nearest and also in relation to determining whether the point lies in a region or a radius.
2- I'm working now to add scopes to the models, so you can pass points as an eloquent or query builder.
3 - if setMainPoint
still gives you an exception please mark this as an issue with description, so I can fix it.
There is no difficulty in implementing some algorithms, but the difficulty lies in returning results to the client code
If you have the time and ability, I welcome any contribution you make.
Beta Was this translation helpful? Give feedback.