返回顶部
m

mapbox-store-locator-patternsMapbox门店定位模式

Common patterns for building store locators, restaurant finders, and location-based search applications with Mapbox. Covers marker display, filtering, distance calculation, and interactive lists.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
96
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

mapbox-store-locator-patterns

门店定位器模式技能

使用Mapbox GL JS构建门店定位器、餐厅查找器和基于位置的搜索应用的全面模式。涵盖标记显示、筛选、距离计算、交互式列表和路线集成。

何时使用此技能

在构建以下应用时使用此技能:

  • - 在地图上显示多个位置(门店、餐厅、办公室等)
  • 允许用户筛选或搜索位置
  • 计算与用户位置的距离
  • 提供与地图标记同步的交互式列表
  • 在弹出窗口或侧面板中显示位置详情
  • 集成到选定位置的路线导航

依赖项

必需:

  • - Mapbox GL JS v3.x
  • @turf/turf - 用于空间计算(距离、面积等)

安装:

bash
npm install mapbox-gl @turf/turf

核心架构

模式概述

典型的门店定位器包含:

  1. 1. 地图显示 - 将所有位置显示为标记
  2. 位置数据 - 包含门店/位置信息的GeoJSON
  3. 交互式列表 - 列出所有位置的侧面板
  4. 筛选 - 搜索、分类筛选、距离筛选
  5. 详情视图 - 显示位置详情的弹出窗口或面板
  6. 用户位置 - 用于距离计算的地理定位。对于蓝点位置指示器,使用内置的mapboxgl.GeolocateControl — 比自定义标记更简单
  7. 路线导航 - 前往选定位置的路线(可选)

数据结构

位置的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上渲染 — 一个,零个逐点DOM元素 |
| 超过1,000个 | 聚类 | 在大比例尺下减少视觉杂乱 |

HTML标记为每个点创建一个DOM元素。超过约100个位置后,浏览器在布局/绘制上花费过多时间。符号图层完全绕过DOM — GPU在单个WebGL绘制调用中绘制所有点。

符号图层实现(最适合100–1,000个位置)。对于HTML标记(少于100个)或聚类(超过1,000个),请参阅references/markers.md。

javascript
map.on(load, () => {
// 添加门店数据作为源
map.addSource(stores, {
type: geojson,
data: stores
});

// 添加自定义标记图像
map.loadImage(/marker-icon.png, (error, image) => {
if (error) throw error;
map.addImage(custom-marker, image);

// 添加符号图层
map.addLayer({
id: stores-layer,
type: symbol,
source: stores,
layout: {
icon-image: custom-marker,
icon-size: 0.8,
icon-allow-overlap: true,
text-field: [get, name],
text-font: [Open Sans Bold, Arial Unicode MS Bold],
text-offset: [0, 1.5],
text-anchor: top,
text-size: 12
}
});
});

// 使用交互API处理标记点击(推荐)
map.addInteraction(store-click, {
type: click,
target: { layerId: stores-layer },
handler: (e) => {
const store = e.feature;
flyToStore(store);
createPopup(store);
}
});

// 或使用传统事件监听器:
// map.on(click, stores-layer, (e) => {
// const store = e.features[0];
// flyToStore(store);
// createPopup(store);
// });

// 悬停时更改光标
map.on(mouseenter, stores-layer, () => {
map.getCanvas().style.cursor = pointer;
});

map.on(mouseleave, stores-layer, () => {
map.getCanvas().style.cursor = ;
});
});

第3步:构建交互式位置列表

javascript
function buildLocationList(stores) {
const listingContainer = document.getElementById(listings);

stores.features.forEach((store, index) => {
const listing = listingContainer.appendChild(document.createElement(div));
listing.id = listing-${store.properties.id};
listing.className = listing;

const link = listing.appendChild(document.createElement(a));
link.href = #;
link.className = title;
link.id = link-${store.properties.id};
link.innerHTML = store.properties.name;

const details = listing.appendChild(document.createElement(div));
details.innerHTML =

${store.properties.address}


${store.properties.phone || }


;

// 处理列表项点击
link.addEventListener(click, (e) => {
e.preventDefault();
flyToStore(store);
createPopup(store);
highlightListing(store.properties.id);
});
});
}

function flyToStore(store) {
map.flyTo({
center: store.geometry.coordinates,
zoom: 15,
duration: 1000
});
}

function createPopup(store) {
const popups = document.getElementsByClassName(mapboxgl-popup);
// 移除现有弹出窗口
if (popups[0]) popups[0].remove();

new mapboxgl.Popup({ closeOnClick: true })
.setLngLat(store.geometry.coordinates)
.setHTML(

${store.properties.name}


${store.properties.address}


${store.properties.phone}


${store.properties.website ? 访问网站 : }
)
.addTo(map);
}

// 重要:highlightListing必须包含scrollIntoView — 没有它,
// 在地图上选择标记不会将侧边栏滚动到该列表项。
function highlightListing(id) {
// 移除现有高亮
const activeItem = document.getElementsByClassName(active);
if (activeItem[0]) {
activeItem[0].classList.remove(active);
}

// 为选中的列表项添加高亮
const listing = document.getElementById(listing-${id});
listing.classList.add(active);

// 将

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 mapbox-store-locator-patterns-1775919123 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 mapbox-store-locator-patterns-1775919123 技能

通过命令行安装

skillhub install mapbox-store-locator-patterns-1775919123

下载

⬇ 下载 mapbox-store-locator-patterns v1.0.0(免费)

文件大小: 18.39 KB | 发布时间: 2026-4-12 10:31

v1.0.0 最新 2026-4-12 10:31
Initial release of mapbox-store-locator-patterns.

- Provides comprehensive patterns and examples for building store locators and location-based search apps using Mapbox GL JS.
- Covers marker strategies, filtering, distance calculation, and interactive lists.
- Includes code snippets for adding maps, markers (HTML, symbol layers, clustering), and interactive location panels.
- Recommends using `@turf/turf` for spatial calculations.
- Details GeoJSON structure and best practices for data handling and UI interactions.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部