Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RaymondE's avatar

Get nearby places Google API

Hi guys,

i don't know how to program something :)

I'm creating a type of a dictionary that includes places.

Similar like this website https://bit.ly/2CWsAgz

How can i program that the nearby entries are found?

The customer journey should be that they enter a location and find all nearby results?

Thanks!

Ray

0 likes
6 replies
hdsavani's avatar

When you add dictionacy with place at that time you have to also add latitude and longitude. So you can get near by you using query like as bellow:

DB::raw("6371 * acos(cos(radians(" . $lat . ")) 
        * cos(radians(posts.lat)) 
        * cos(radians(posts.lon) - radians(" . $lon . ")) 
        + sin(radians(" .$lat. ")) 
        * sin(radians(posts.lat))) AS distance")

Simple Full Example: https://itsolutionstuff.com/post/how-to-get-latitude-longitude-from-mysql-in-laravel-5example.html

RaymondE's avatar

Thanks for the answers, they are helping me find a solution!

This is how i would set up my project now:

User types in location -> Google API gets all nearby cities/villages from this location in a certain range and puts them in a array -> This array is then matched with all my entries

Would this be a performance issue? I have planed to have more than 1.000 entries and a "nearby" array could have 20 or 40 city/villages.

Or would it be easier to calculate the distance based on the coordinates, like @hdsavani suggests?

Thanks guys!

mehdidouyeb's avatar

Hi guys my goal is to retrieve all the restaurants in a given city. The problem with Places Api is that I don't see how I can get ALL of them. First we are restricted by a certain amount and second we can only do a search near by so that means that we should divide the city in a lot of nearby searchs. Do you have any suggestions ? Thanks

Please or to participate in this conversation.