malhal / Laravel-Geographical

Easily add longitude and latitude columns to your records and use inherited functionality for calculating distances
MIT License
281 stars 49 forks source link

Get two distances #41

Open dreigningking opened 3 days ago

dreigningking commented 3 days ago

I really love this package and I want to get two separate distances on my model based on the proximity to two different locations. Please how can I do this

malhal commented 3 days ago

Hi thanks, how about doing 2 separate queries?

dreigningking commented 3 days ago

I'm not sure two separate queries will work. What I want to do is this.

I have a model called Package, and it has origin latitude, origin longitude, destination latitude and destination longitude.

Then I have another model called Location, which has latitude and longitude.

So I get one package, e g with id 1 And I want to get all locations that can send and receive the package, ordering them by kilometer distance from origin and kilometer distance from destination.

$locations = Location::distance1($package->origin_latitude,$package->origin_longitude) ->distance2($package->destination_latitude, $package->destination_longitude)->orderBy("distance1","asc")->orderBy("distance2","asc")->get()

Any help?

dreigningking commented 2 days ago

Any help ?

malhal commented 2 days ago

A couple of ideas:

This plugin is about finding the distance from a lat/lon that is user supplied to models, i.e. a lat/lon that is not already stored inside the database. Your use case makes me think all of these distances could already be calculated, since you said the Package and Location are already models thus all of their lat/lons are already in the database. In which case it would just be a case of creating the relationships correctly.

if Location is just a lat/lon then perhaps Package could have origin Location and destination Location, i.e. use relations instead of copying lat/lon double.s

dreigningking commented 2 days ago

let me give you a scenerio. Mike wants to send a package from china town in chicago (origin) to miami in florida (destination_ So package has [id, origin_latitude, origin_longitude, destination_latitude, destination_longitude]

Then mike checks for which courier can help me send it.

Now i want to show mike all the courier operating in those two region, but i want to also sort them for Mike, so that he can see the courier companies in chicago that are close to china town first, and also close to miami (like sorting with distance to origin and distance to destination)

That's the reason for this: $locations = Location::distance1(china_town_lat&long) ->distance2(miami_lat&long)->orderBy("distance_to_china_town","asc")->orderBy("distance_to_miami","asc")->get()

dreigningking commented 10 hours ago

Please I'm still waiting, any assistance ?