Geolocation and finding the closest dropoff point to a coordinate

John Harlan, over 1 year ago

Everybody's Got Problems

In consulting, one of the best parts of our job is getting to see all the interesting and sometimes unique problems high-growth businesses face. While rebuilding Live Well 30A's eCommerce site and platform, one of the biggest issues they faced was figuring out where to put people's beach chairs. When they first told us this, we didn't understand how this could be difficult. There's a beach and you put the chairs on the beach. Well, the problem is that it's a big beach. Twenty miles long with fifty different 'access points'. Combine that with 1,500 customers spread out all over those 20 miles, well now we see the problem.

Such A Simple Answer

We love when the answer is really simple. We solved the problem for them with one new column to their orders table, one new database table, and eight lines of code.

  • Create a BeachAccess table with a list of addresses and geocodes for each access point (geocodes came from Google API)
  • Take the geocode for each order with beach chairs and use some basic geometry to find the closest one
def assign_beach_access
    results = {}
    BeachAccess.all.each do |beach|
        results[] = Math.sqrt(( - lat)**2 + (beach.lng - lng)**2)
    result = results.min_by{|k,v| v}
    shipment.update(beach_access_id: result.first)
  • Add a link in their mobile app showing the customer exactly where their chairs will be
  • Automatically build the route for the delivery team based on the results

Such a simple solution literally saves the Live Well team hours each day. Caveat: If you had thousands or millions of points to test against, this would be a bad idea. You would want to cut the set down first before iterating over each point. We only have fifty different points and valued the simplicity of the code over a more involved solution.