Mapbox Search Integration Skill
Expert guidance for implementing Mapbox search functionality in applications. Covers the complete workflow from asking the right discovery questions, selecting the appropriate search product, to implementing production-ready integrations following best practices from the Mapbox search team.
Use This Skill When
User says things like:
- - "I need to add search to my map"
- "I need a search bar for my mapping app"
- "How do I implement location search?"
- "I want users to search for places/addresses"
- "I need geocoding in my application"
This skill complements mapbox-search-patterns:
- -
mapbox-search-patterns = Tool and parameter selection - INLINECODE2 = Complete implementation workflow
Discovery Phase: Ask the Right Questions
Before jumping into code, ask these questions to understand requirements:
Question 1: What are users searching for?
Ask: "What do you want users to search for?"
Common answers and implications:
- - "Addresses" → Use Search Box API (the default for interactive address search, including geocoding). Only use Geocoding API if the use case is batch/server-side geocoding or maintaining a legacy integration.
- "Points of interest / businesses" → POI search, use Search Box API with category search
- "Both addresses and POIs" → Search Box API
- "Specific types of POIs" (restaurants, hotels, etc.) → Search Box API
- "Countries, cities, postcodes or neighborhoods" → Search Box API for interactive search; Geocoding API only for batch/server-side geocoding
- "Custom locations" (user-created places) → May need custom data + search integration
Follow-up if not stated initially: "Are your users searching for points of interest data? Restaurants, stores, categories of businesses?"
Implications:
- - "Yes, POIs are included" → Use the Search Box API
- "No, the user does not need POI search" → Still default to Search Box API for interactive/autocomplete use cases. Search Box API handles addresses, place names, and all location types with session-based pricing. Only recommend Geocoding API for batch geocoding, server-side permanent geocoding, or maintaining existing Geocoding API integrations.
Question 2: What's the geographic scope?
Ask: "Where will users be searching?"
Common answers and implications:
- - "Single country" (e.g., "only USA") → Use
country parameter, better results, lower cost - "Specific region" → Use
bbox parameter for bounding box constraint - "Global" → No country restriction, but may need language parameter
- "Multiple specific countries" → Use
country array parameter
Follow-up: "Do you need to limit results to a specific area?" (delivery zone, service area, etc.)
Question 3: What's the search interaction pattern?
Ask: "How will users interact with search?"
Common answers and implications:
- - "Search-as-you-type / autocomplete" → Use Search Box API with
auto_complete: true and session-based pricing (most cost-efficient for autocomplete). Implement debouncing. - "Search button / final query" → Can use either API, no autocomplete needed
- "Both" (autocomplete + refine) → Two-stage search, autocomplete then detailed results
- "Voice input" → Consider speech-to-text integration, handle longer queries
Question 4: What platform?
Ask: "What platform is this for?"
Common answers and implications:
- - "Web application" → Mapbox Search JS (easiest), or direct API calls for advanced cases
- "iOS app" → Search SDK for iOS (recommended), or direct API integration for advanced cases
- "Android app" → Search SDK for Android (recommended), or direct API integration for advanced cases
- "Multiple platforms" → Platform-specific SDKs (recommended), or direct API approach for consistency
- "React app" → Mapbox Search JS React (easiest with UI), or Search JS Core for custom UI. Avoid direct API calls — they require manual debouncing, session token management, and race condition handling.
- "Vue / Angular / Other framework" → Mapbox Search JS Core or Web. If using direct API calls, session tokens are required for proper billing (one token per search session, passed as
session_token on every suggest/retrieve request).
Question 5: How will results be used?
Ask: "What happens when a user selects a result?"
Common answers and implications:
- - "Fly to location on map" → Need coordinates, map integration
- "Show details / info" → Need to retrieve and display result properties
- "Fill form fields" → Need to parse address components
- "Start navigation" → Need coordinates, integrate with directions
- "Multiple selection" → Need to handle selection state, possibly show markers
Question 6: Expected usage volume?
Ask: "How many searches do you expect per month?"
Implications:
- - Low volume (< 10k) → Free tier sufficient, simple implementation
- Medium volume (10k-100k) → Consider caching, optimize API calls
- High volume (> 100k) → Implement debouncing, caching, batch operations, monitor costs
Product Selection Decision Tree
Based on discovery answers, recommend the right product:
Key principle: Search Box API is the default choice for virtually all interactive search use cases, including address search, geocoding, autocomplete, and POI search. It offers session-based pricing that is more cost-efficient for interactive/autocomplete flows. Only recommend Geocoding API for the narrow cases listed below.
Search Box API (DEFAULT)
Use when (any of these):
- - User needs interactive address search or autocomplete (this IS geocoding — Search Box API handles it)
- User needs POI / category search
- User needs any end-user-facing search UI
- User wants session-based pricing (more cost-efficient for autocomplete/interactive use)
- User is building a web, iOS, or Android app with a search bar
Prefer SDKs over direct API calls for web integration:
- - Mapbox Search JS (SDK) - Recommended for web integration, with three components:
-
Search JS React - Easy search integration via React library with UI
-
Search JS Web - Easy search integration via Web Components with UI
-
Search JS Core - JavaScript (node or web) wrapper for API, build your own UI
- - Search Box API (REST) - Direct API integration, for advanced/custom cases
- Search SDK for iOS - Native iOS integration
- Search SDK for Android - Native Android integration
Geocoding API (SPECIALIZED)
Use ONLY when:
- - Batch geocoding large lists of addresses (server-side)
- Permanent/stored geocoding results (server-side, where results are persisted)
- Maintaining an existing Geocoding API integration (migration not justified)
- No interactive/user-facing search needed
Do NOT recommend Geocoding API when:
- - The user wants a search bar, autocomplete, or interactive address lookup — use Search Box API instead
- The user says "geocoding" but describes an interactive search flow — use Search Box API instead
Reference Files
Load the relevant reference based on the user's platform and needs:
- - Web (Search JS React / Web / Core / Direct API) → Load INLINECODE8
- When: User is building a web app (vanilla JS, any framework except React-specific patterns)
- - React Integration → Load INLINECODE9
- When: User is building a React app specifically
- - iOS → Load INLINECODE10
- When: User is building an iOS app (Swift/UIKit/SwiftUI)
- - Android → Load INLINECODE11
- When: User is building an Android app (Kotlin/Java)
- - Node.js → Load INLINECODE12
- When: User needs server-side search (Express, serverless, backend API)
- - Best Practices → Load INLINECODE13
- When: Implementing search for the first time, or optimizing an existing implementation
- Covers: debouncing, session tokens, geographic filtering, error handling, accessibility, caching, token security
- - Common Pitfalls → Load INLINECODE14
- When: Debugging issues, reviewing code, or during code review
- Covers: no debouncing, missing session tokens, no geo context, poor mobile UX, race conditions
- - Framework Hooks → Load INLINECODE15
- When: Building custom hooks (React) or composables (Vue) around Search JS Core
- - Testing and Monitoring → Load INLINECODE16
- When: Writing tests or setting up production monitoring/analytics
Checklist: Production-Ready Search
Before launching, verify:
Configuration:
- - [ ] Token properly scoped (search:read only)
- [ ] URL restrictions configured
- [ ] Geographic filtering set (country, proximity, or bbox)
- [ ] Types parameter set based on use case
- [ ] Language parameter set if needed
Implementation:
- - [ ] Debouncing implemented (300ms recommended)
- [ ] Session tokens used correctly
- [ ] Error handling for all failure cases
- [ ] Loading states shown
- [ ] Empty results handled gracefully
- [ ] Race conditions prevented
UX:
- - [ ] Touch targets at least 44pt/48dp
- [ ] Results show enough context (name + address)
- [ ] Keyboard navigation works
- [ ] Accessibility attributes set
- [ ] Mobile keyboard handled properly
Performance:
- - [ ] Caching implemented (if high volume)
- [ ] Request timeout set
- [ ] Minimal data fetched
- [ ] Bundle size optimized
Testing:
- - [ ] Unit tests for core logic
- [ ] Integration tests with real API
- [ ] Tested on slow networks
- [ ] Tested with various query types
- [ ] Mobile device testing
Monitoring:
- - [ ] Analytics tracking set up
- [ ] Error logging configured
- [ ] Usage monitoring in place
- [ ] Budget alerts configured
Integration with Other Skills
Works with:
- - mapbox-search-patterns: Parameter selection and optimization
- mapbox-web-integration-patterns: Framework-specific patterns
- mapbox-token-security: Token management and security
- mapbox-web-performance-patterns: Optimizing search performance
Resources
-
Search JS React
-
Search JS Web
-
Search JS Core
Quick Decision Guide
User says: "I need location search"
- 1. Ask discovery questions (Questions 1-6 above)
- Recommend product:
-
Search Box API (default for all interactive/user-facing search, including address geocoding)
- Geocoding API only for batch/server-side/permanent geocoding
- Platform SDK preferred (Search JS for web, native SDKs for mobile)
- 3. Implement with:
- ✅ Debouncing
- ✅ Session tokens
- ✅ Geographic filtering
- ✅ Error handling
- ✅ Good UX
- 4. Test thoroughly
- Monitor in production
Remember: The best search implementation asks the right questions first, then builds exactly what the user needs - no more, no less.
Mapbox 搜索集成技能
在应用程序中实现 Mapbox 搜索功能的专家指导。涵盖从提出正确的发现性问题、选择适当的搜索产品,到按照 Mapbox 搜索团队的最佳实践实现生产级集成的完整工作流程。
何时使用此技能
当用户说出类似以下内容时:
- - 我需要在地图上添加搜索功能
- 我需要为我的地图应用添加搜索栏
- 如何实现位置搜索?
- 我希望用户能够搜索地点/地址
- 我需要在应用程序中实现地理编码
此技能与 mapbox-search-patterns 互补:
- - mapbox-search-patterns = 工具和参数选择
- mapbox-search-integration = 完整的实现工作流程
发现阶段:提出正确的问题
在开始编码之前,提出以下问题以了解需求:
问题 1:用户要搜索什么?
提问:您希望用户搜索什么?
常见答案及其含义:
- - 地址 → 使用 Search Box API(交互式地址搜索的默认选项,包括地理编码)。仅当用例是批量/服务器端地理编码或维护旧版集成时才使用 Geocoding API。
- 兴趣点/商家 → POI 搜索,使用带有分类搜索功能的 Search Box API
- 地址和 POI 两者 → Search Box API
- 特定类型的 POI(餐厅、酒店等)→ Search Box API
- 国家、城市、邮政编码或社区 → 交互式搜索使用 Search Box API;仅批量/服务器端地理编码使用 Geocoding API
- 自定义位置(用户创建的地点)→ 可能需要自定义数据 + 搜索集成
如果最初未说明,可跟进提问:您的用户是否在搜索兴趣点数据?餐厅、商店、商家类别?
含义:
- - 是的,包含 POI → 使用 Search Box API
- 不,用户不需要 POI 搜索 → 仍然默认使用 Search Box API 用于交互式/自动补全用例。Search Box API 处理地址、地名和所有位置类型,采用基于会话的定价。仅当需要批量地理编码、服务器端永久地理编码或维护现有 Geocoding API 集成时才推荐使用 Geocoding API。
问题 2:地理范围是什么?
提问:用户将在哪里进行搜索?
常见答案及其含义:
- - 单一国家(例如,仅限美国)→ 使用 country 参数,结果更佳,成本更低
- 特定区域 → 使用 bbox 参数进行边界框约束
- 全球范围 → 无国家限制,但可能需要语言参数
- 多个特定国家 → 使用 country 数组参数
跟进提问:您是否需要将结果限制在特定区域?(配送区域、服务区域等)
问题 3:搜索交互模式是什么?
提问:用户将如何与搜索进行交互?
常见答案及其含义:
- - 即输即搜/自动补全 → 使用 Search Box API,设置 auto_complete: true,采用基于会话的定价(对自动补全最具成本效益)。实现防抖处理。
- 搜索按钮/最终查询 → 可使用任一 API,无需自动补全
- 两者兼顾(自动补全 + 细化)→ 两阶段搜索,先自动补全再显示详细结果
- 语音输入 → 考虑语音转文本集成,处理较长查询
问题 4:使用什么平台?
提问:这是为哪个平台开发的?
常见答案及其含义:
- - Web 应用程序 → Mapbox Search JS(最简单),或针对高级用例的直接 API 调用
- iOS 应用 → iOS 版 Search SDK(推荐),或针对高级用例的直接 API 集成
- Android 应用 → Android 版 Search SDK(推荐),或针对高级用例的直接 API 集成
- 多平台 → 平台特定 SDK(推荐),或为保持一致性采用直接 API 方式
- React 应用 → Mapbox Search JS React(带 UI 最简单),或 Search JS Core 用于自定义 UI。避免直接 API 调用——它们需要手动防抖、会话令牌管理和竞态条件处理。
- Vue / Angular / 其他框架 → Mapbox Search JS Core 或 Web。如果使用直接 API 调用,则需要会话令牌以进行正确计费(每个搜索会话一个令牌,在每个建议/检索请求中作为 session_token 传递)。
问题 5:结果将如何使用?
提问:当用户选择结果时会发生什么?
常见答案及其含义:
- - 飞至地图上的位置 → 需要坐标、地图集成
- 显示详情/信息 → 需要检索并显示结果属性
- 填充表单字段 → 需要解析地址组件
- 开始导航 → 需要坐标,与导航集成
- 多选 → 需要处理选择状态,可能显示标记
问题 6:预期使用量?
提问:您预计每月有多少次搜索?
含义:
- - 低量(< 1万)→ 免费套餐足够,实现简单
- 中量(1万-10万)→ 考虑缓存,优化 API 调用
- 高量(> 10万)→ 实现防抖、缓存、批量操作,监控成本
产品选择决策树
根据发现阶段的答案,推荐合适的产品:
关键原则:Search Box API 是几乎所有交互式搜索用例的默认选择,包括地址搜索、地理编码、自动补全和 POI 搜索。它采用基于会话的定价,对交互式/自动补全流程更具成本效益。仅在下文列出的狭窄情况下推荐使用 Geocoding API。
Search Box API(默认)
在以下情况使用(满足任一条件):
- - 用户需要交互式地址搜索或自动补全(这本身就是地理编码——Search Box API 可以处理)
- 用户需要 POI / 分类搜索
- 用户需要任何面向最终用户的搜索 UI
- 用户希望采用基于会话的定价(对自动补全/交互式使用更具成本效益)
- 用户正在构建带有搜索栏的 Web、iOS 或 Android 应用
对于 Web 集成,优先使用 SDK 而非直接 API 调用:
- - Mapbox Search JS(SDK)- 推荐用于 Web 集成,包含三个组件:
-
Search JS React - 通过 React 库轻松集成搜索,带 UI
-
Search JS Web - 通过 Web Components 轻松集成搜索,带 UI
-
Search JS Core - API 的 JavaScript(Node 或 Web)封装,可构建自定义 UI
- - Search Box API(REST)- 直接 API 集成,适用于高级/自定义用例
- iOS 版 Search SDK - 原生 iOS 集成
- Android 版 Search SDK - 原生 Android 集成
Geocoding API(专用)
仅当以下情况使用:
- - 批量地理编码大量地址列表(服务器端)
- 永久/存储地理编码结果(服务器端,结果需要持久化)
- 维护现有的 Geocoding API 集成(迁移成本不合理)
- 不需要交互式/面向用户的搜索
在以下情况不要推荐 Geocoding API:
- - 用户需要搜索栏、自动补全或交互式地址查找——改用 Search Box API
- 用户提到地理编码但描述的是交互式搜索流程——改用 Search Box API
参考文件
根据用户的平台和需求加载相关参考文件:
- - Web(Search JS React / Web / Core / 直接 API) → 加载 references/web-search-js.md
- 适用场景:用户正在构建 Web 应用(原生 JS,除 React 特定模式外的任何框架)
- - React 集成 → 加载 references/react-search.md
- 适用场景:用户专门构建 React 应用
- - iOS → 加载 references/ios-search.md
- 适用场景:用户正在构建 iOS 应用(Swift/UIKit/SwiftUI)
- - Android → 加载 references/android-search.md
- 适用场景:用户正在构建 Android 应用(Kotlin/Java)
- - Node.js → 加载 references/nodejs-search.md
- 适用场景:用户需要服务器端搜索(Express、无服务器、后端 API)
- - 最佳实践 → 加载 references/best-practices.md
- 适用场景:首次实现搜索,或优化现有实现
- 涵盖:防抖、会话令牌、地理过滤、错误处理、可访问性、缓存、令牌安全
- - 常见陷阱 → 加载 references/pitfalls.md
- 适用场景:调试问题、审查代码或进行