Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Ability to find nearby/close coordinate points #4

HassanZahirnia started this conversation in Ideas
Discussion options

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] ]
You must be logged in to vote

Replies: 2 comments 4 replies

Comment options

Great, this going to be in v2.1.0 after a few days.

You must be logged in to vote
2 replies
Comment options

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.

Comment options

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.

Comment options

@HassanZahirnia
You can check the new release now! (v2.1.0)

You must be logged in to vote
2 replies
Comment options

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:

  1. Currently with the way the setPoints is implemented, it's not possible to pass eloquent models or database rows that include a longitude and latitude attribute/column, as the setPoints function only accepts an array of coordinates in format of [lat, lng]. One may say extracting the coordinates from eloquent and passing it to setPoints is no problem and I agree with that, but the thing is, after getOrderByNearestNeighbor returns the result, you'd have no idea which coordinate belongs to which model/record. So you'd have to write a loop including where clauses to trace back the coordinates to the relevant model
  2. I'm getting a Non static method 'setMainPoint' should not be called statically.intelephense(1036) just wanted to put this out there.
  3. I think getOrderByNearestNeighbor should be renamed to getNearestNeighbors 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
)
  1. Also this idea occurred to me, maybe the NearestNeighbors function could also take a radius 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

Comment options

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.

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

AltStyle によって変換されたページ (->オリジナル) /