Store Locator Patterns Skill
Comprehensive patterns for building store locators, restaurant finders, and location-based search applications with Mapbox GL JS. Covers marker display, filtering, distance calculation, interactive lists, and directions integration.
When to Use This Skill
Use this skill when building applications that:
- - Display multiple locations on a map (stores, restaurants, offices, etc.)
- Allow users to filter or search locations
- Calculate distances from user location
- Provide interactive lists synced with map markers
- Show location details in popups or side panels
- Integrate directions to selected locations
Dependencies
Required:
- - Mapbox GL JS v3.x
- @turf/turf - For spatial calculations (distance, area, etc.)
Installation:
CODEBLOCK0
Core Architecture
Pattern Overview
A typical store locator consists of:
- 1. Map Display - Shows all locations as markers
- Location Data - GeoJSON with store/location information
- Interactive List - Side panel listing all locations
- Filtering - Search, category filters, distance filters
- Detail View - Popup or panel with location details
- User Location - Geolocation for distance calculation. For the blue dot location indicator, use the built-in
mapboxgl.GeolocateControl — simpler than custom markers. - Directions - Route to selected location (optional)
Data Structure
GeoJSON format for locations:
CODEBLOCK1
Key properties:
- -
id - Unique identifier for each location - INLINECODE2 - Display name
- INLINECODE3 - Full address for display and geocoding
- INLINECODE4 -
[longitude, latitude] format - INLINECODE6 - For filtering (retail, restaurant, office, etc.)
- Custom properties as needed (hours, phone, website, etc.)
Basic Store Locator Implementation
Step 1: Initialize Map and Data
CODEBLOCK2
Step 2: Add Markers to Map
Marker strategy by location count:
| Count | Strategy | Reason |
|---|
| Fewer than 100 | HTML Markers | Full DOM/CSS control; DOM node count is manageable |
| 100–1,000 |
Symbol Layer (default) | Renders on the
GPU via WebGL — one
<canvas>, zero per-point DOM elements |
|
More than 1,000 | Clustering | Reduces visual clutter at large scale |
HTML Markers create one DOM element per point. Beyond ~100 locations the browser spends too much time on layout/paint. Symbol layers bypass the DOM entirely — the GPU draws all points in a single WebGL draw call.
Symbol Layer implementation (best for 100–1,000 locations). For HTML Markers (fewer than 100) or Clustering (more than 1,000), see references/markers.md.
CODEBLOCK3
Step 3: Build Interactive Location List
CODEBLOCK4
Reference Files
Load these references for additional patterns as needed:
| Reference | File | Contents |
|---|
| HTML Markers & Clustering | INLINECODE9 | HTML Markers (< 100 locations), Clustering (> 1000 locations) |
| Search & Filter |
references/search-filter.md | Text search, category filter |
| Geolocation & Directions |
references/geolocation-directions.md | User location, distance calculation, route directions |
| Styling & Layout |
references/styling-layout.md | Full HTML/CSS layout, custom marker CSS |
| Performance & A11y |
references/optimization-a11y.md | Debounced search, data management, error handling, accessibility |
| Variations & React |
references/variations-react.md | Mobile-first, fullscreen, map-only, React implementation |
Resources
门店定位器模式技能
使用Mapbox GL JS构建门店定位器、餐厅查找器和基于位置的搜索应用的全面模式。涵盖标记显示、筛选、距离计算、交互式列表和路线集成。
何时使用此技能
在构建以下应用时使用此技能:
- - 在地图上显示多个位置(门店、餐厅、办公室等)
- 允许用户筛选或搜索位置
- 计算与用户位置的距离
- 提供与地图标记同步的交互式列表
- 在弹出窗口或侧面板中显示位置详情
- 集成到选定位置的路线导航
依赖项
必需:
安装:
bash
npm install mapbox-gl @turf/turf
核心架构
模式概述
典型的门店定位器包含:
- 1. 地图显示 - 将所有位置显示为标记
- 位置数据 - 包含门店/位置信息的GeoJSON
- 交互式列表 - 列出所有位置的侧面板
- 筛选 - 搜索、分类筛选、距离筛选
- 详情视图 - 显示位置详情的弹出窗口或面板
- 用户位置 - 用于距离计算的地理定位。对于蓝点位置指示器,使用内置的mapboxgl.GeolocateControl — 比自定义标记更简单
- 路线导航 - 前往选定位置的路线(可选)
数据结构
位置的GeoJSON格式:
json
{
type: FeatureCollection,
features: [
{
type: Feature,
geometry: {
type: Point,
coordinates: [-77.034084, 38.909671]
},
properties: {
id: store-001,
name: 市中心门店,
address: 123 Main St, Washington, DC 20001,
phone: (202) 555-0123,
hours: 周一至周六:上午9点-晚上9点,周日:上午10点-下午6点,
category: 零售,
website: https://example.com/downtown
}
}
]
}
关键属性:
- - id - 每个位置的唯一标识符
- name - 显示名称
- address - 用于显示和地理编码的完整地址
- coordinates - [经度, 纬度]格式
- category - 用于筛选(零售、餐厅、办公室等)
- 根据需要自定义属性(营业时间、电话、网站等)
基本门店定位器实现
第1步:初始化地图和数据
javascript
import mapboxgl from mapbox-gl;
import mapbox-gl/dist/mapbox-gl.css;
mapboxgl.accessToken = YOURMAPBOXACCESS_TOKEN;
// 门店位置数据
const stores = {
type: FeatureCollection,
features: [
{
type: Feature,
geometry: {
type: Point,
coordinates: [-77.034084, 38.909671]
},
properties: {
id: store-001,
name: 市中心门店,
address: 123 Main St, Washington, DC 20001,
phone: (202) 555-0123,
category: 零售
}
}
// ... 更多门店
]
};
const map = new mapboxgl.Map({
container: map,
style: mapbox://styles/mapbox/standard,
center: [-77.034084, 38.909671],
zoom: 11
});
第2步:在地图上添加标记
按位置数量划分的标记策略:
| 数量 | 策略 | 原因 |
|---|
| 少于100个 | HTML标记 | 完全的DOM/CSS控制;DOM节点数量可控 |
| 100–1,000个 |
符号图层(默认) | 通过
WebGL在GPU上渲染 — 一个