last day (15 days later) » 

10:24 PM
0
Q: Laravel chunk filter collection

Mathieu MourareauActually i have a simple collection but i need to make a filter to this collection and the filter call GoogleMaps to determine the distance between origin and destination, the problem is that i have more than 100 row $arbitres and this part of the process is getting very very very long. How coul...

 
I wouldn't think that calling an external API at runtime for 100+ requests is a good option. Could you use linear distance instead of the driving distance?
 
what do you mean by linear distance, linear distance from the api ? yes i agree to really not good
 
It looks like you're calculating the driving distance between two points. Could you use the real linear (straight point-to-point) distance instead? If yes, that opens up other options that are far more scaleable.
 
between two addresses :(
 
I don't understand.
 
10:24 PM
I get two address like zip code city ect ... not to points latitude and longitude! It’s a problem I guess no ?
 
Why can't you just geolocate the addresses when you add them to your database, then geolocate the single origin point at runtime, and using math, calculate the linear distance between then?
 
hi man thanks for your help
how i could achieve that ? i never done that before
 
Google has another API for geolocating an address to a latitude and longitude point. When you add a new address to your database, use that API to lookup the location, and store the positional coordinate with the address.
When performing a search, you'll need to use the same API to look up the latitude and longitude of the origin point.
 
and you think it gonna be faster ?
 
Yes. You will only be looking up one point at runtime. The rest will already be stored in your database.
You can then just use math to calculate the distance between the origin and each point stored in your database using something like this: stackoverflow.com/questions/27928/…
Although there are hundreds of examples online.
I would suggest running the calculation at the database level so that you only return addresses that are within the distance you want. E.g. < 20km.
 
10:32 PM
yes i will do that because with my solution it's getting so long
what do you mean by You will only be looking up one point at runtime ?
 
In your question you were measuring the distance between an origin and destination.
Are the destinations coming from your database?
 
yes origin and destination are stored in the database
for exemple : origin = 1 rue du fort, 31320 Castanet-Tolosan and $destination = 1 avenue du general leclerc 11000 Carcassonne
 
Oh, if they're both already stored in the database, that makes the problem even easier, because you can lookup the lat/lng coordinates once when you first store the address.
So now you're just calculating the distance between them and removing/limiting the results based on that.
 
so i steel need to convert the $origin and $destination to lat/lng ?
 
Yes. But the addresses will never (rarely) change, so you can lookup the lat/lng once and store it with the address in the database.
 
10:44 PM
to convert i need to use google api right ?
in convert all my addresses then in my loop i check lat/lng and math for distance right ?
 
You could use Google API, or there are other sources too.
And no, it's wasteful to select all data and process it at the application level when the same result can be achieved at the database level.
 
ok i will do that many thanks for your help :)
 
No worries. Good luck.
 

last day (15 days later) »