Search the company's product closest to the customer

Friends,

I will need to implement a search considering the distance between user and company. Several companies can have the same product, when the user performs the search, the closest companies that registered the product will appear first.

I saw this Google api: https://developers.google.com/maps/solutions/store-locator/clothing-store-locator?csw=1

Has anyone used it? Has anyone done something like that? If any friend can give a direction or inform if they have seen something in cake I appreciate it :+1:t3:

I have not done this but give you an idea, you can compare the user location with store location by using
logitute and latitute. and show the nearest store where product stock is available.

If you have the coordinates of both the user and stores, you can use this approach, simple select the distance as a column (calculate it using functions) and sort the results by that column.

If you don’t have the coordinates, you can get it using Google geocode API (AFAIK not free). Just make a shell/command that consume that API in a cronjob

1 Like

As far I can see the document you’re linking to shows a ‘as the crow flies’ solution. In real world you need traveldistances/time, depending on chosen transport /time of day, pretty complicated. Don’t think you want to develop that sort of things. Depending on how much time/money you can spend, you better look for services which suits your needs.

If ‘as the crow flies’ like solution are ok, I like to use openstreetmap & leaflet instead of google maps because of its opensource nature and geographic features of a database instead of having to develop functions/queries with cos/sin/radians etc. like the given:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) )AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

But this is offcourse independent of the use of CakePHP, CakePHP will not be the problem to glue things together.

Thanks for the feedback. I started to implement this. I’m using this plugin.

Meets what I need, but I’m getting 400 status. I get an error in this line of code:

   $location = $this->request->getQuery('location');

 =>  $geocodeResult = $this->Geocoder->geocode($location);

I will save the coordinates when registering the client. And then I’ll do the search for the approach. I appreciate any comments!