Skip to main content

Nearest Store Search

Problem: A customer enters an address. Show them the nearest store — by drive time, not by circle.
A store 800 metres away across a river, a rail line, or a limited-access highway is not close.Ranking by straight-line distance is the defect that gets store locators rebuilt.

The pattern

Two stages. The first is free. Geocode the customer address → 1 API call (usually a cache hit) ST_DWithin: k nearest by distance → 0 API calls. PostGIS. Matrix: origin × k, drive time → 1 API call Sort by travel time → 0 API calls Straight-line distance is a bad ranker and an excellent filter. It narrows 2,000 stores to 10 for free. Matrix ranks those 10 correctly.
Cost is bounded by k, not by store count. A 5,000-store network and a 50-store network cost the same per lookup.

Prerequisites

  • HERE API key with Geocoding & Search and Matrix Routing entitlement
  • PostGIS with your stores in an indexed geography column
  • export HERE_API_KEY="..."

The code

Python

Schema

Why not query HERE for your stores?

Your stores live in your database.Querying HERE’s public place index for them is fragile and expensive, and it will silently miss the store that opened last week — map data ships on a release cadence. Your store table does not.
Hours, services, and inventory change hourly. No mapping API knows them.

Common mistakes

Ranking by straight-line distance. Rivers. Matrix over the entire store network instead of a shortlist. k routing calls to rank instead of one matrix call. Querying /discover or /browse for your own stores. Silently expanding the radius until a result appears. A locator that cheerfully sends people 200 miles. Routing to position instead of access. Computing directions on page render. Most visitors never click. Directions on click only. Not caching the ranking. For a user coordinate rounded to ~3 decimal places, the drive-time ranking of nearby stores is stable for weeks. No GIST index. Sequential scan over stores. Ignoring matrix errorCodes. 3 means a route was found but it violates a restriction.

Production considerations

Cache the ranked result, keyed on rounded user coordinate. Dense urban areas produce enormous hit rates. Geocode cache hit rate approaches 1. Users type city names and ZIP codes, not distinct street addresses. Directions on click, not on render. CDN in front of tiles. Filter by business rules in SQL, before the matrix call. open_now, has_pharmacy, in_stock — your data, free. Restrict browser-exposed keys by domain. Target cost structure: bounded per lookup, independent of network size, dominated by cache misses.

Store Locator

The full architecture, and the build-vs-buy decision.

Distance Matrix

Modes, ceilings, and the flat array.

Geocode an Address

access vs position, and confidence scoring.

Routing vs Matrix

Why k routing calls is the wrong primitive.

HERE documentation

Placematic


Need production HERE API keys or implementation support? Placematic is an official HERE Technologies reseller and implementation partner. Talk to us.