Communitygithub.com

地图与地理定位

整合 Mapbox/Leaflet,提供地理编码、店铺定位、标记聚类和路线显示功能。

地图与地理定位 是什么?

该技能为 AI Agent 提供全面的地图与地理定位集成能力。它支持 Mapbox GL JS(通过 `react-map-gl` 实现 React 封装)和 Leaflet(通过 `react-leaflet` 实现 React 封装,无需 API 密钥即可使用基础瓦片)两种主流地图库。技能涵盖了从地理编码(地址转坐标,支持 Mapbox Geocoding API 或免费的 OpenStreetMap Nominatim)、标记聚类(使用 `supercluster` 或 Mapbox 内置聚类处理 100+ 标记)、店铺定位模式(地理编码用户位置 → 使用 Haversine/PostGIS 查询附近店铺 → 在地图上显示)到路线显示(使用 Mapbox Directions API 生成驾车/步行路线,渲染为 GeoJSON LineString 图层)等完整开发流程。同时强调最佳实践,如使用 `NEXT_PUBLIC_MAPBOX_TOKEN` 管理客户端 API 密钥、使用 `dynamic(() => import("./Map"), { ssr: false })` 惰性加载地图组件、始终处理浏览器地理定位权限拒绝并提供回退方案等。该技能适用于 Cursor、Claude Code 等 AI Agent 平台,帮助开发者快速构建高性能、用户友好的地理定位功能。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/maps-geolocation

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

地图与地理定位 是做什么的?

规则

  • Mapbox GL JS 用于丰富的交互式地图 — react-map-gl 作为 React 封装
  • Leaflet 作为免费替代方案 — react-leaflet 用于 React,基础瓦片无需 API 密钥
  • API 密钥管理:NEXT_PUBLIC_MAPBOX_TOKEN 用于客户端 Mapbox,切勿暴露服务器端密钥
  • 地理编码:将地址转换为坐标 — Mapbox Geocoding API 或 OpenStreetMap Nominatim(免费)
  • 标记聚类:使用 supercluster 或 Mapbox 内置聚类处理大数据集标记(100+)
  • 店铺定位模式:地理编码用户位置 → 使用 Haversine/PostGIS 查询附近店铺 → 在地图上显示
  • 路线显示:Mapbox Directions API 用于驾车/步行路线,渲染为 GeoJSON LineString 图层
  • 浏览器地理定位:navigator.geolocation.getCurrentPosition() — 始终优雅处理权限拒绝
  • 惰性加载地图组件 — 地图较重;使用 dynamic(() => import("./Map"), { ssr: false })

模式

// react-map-gl 与 Mapbox
import Map, { Marker, NavigationControl } from "react-map-gl";
import "mapbox-gl/dist/mapbox-gl.css";

function StoreMap({ stores }: { stores: Store[] }) {
  return (
    <Map
      initialViewState={{ longitude: -122.4, latitude: 37.8, zoom: 11 }}
      mapboxAccessToken={process.env.NEXT_PUBLIC_MAPBOX_TOKEN}
      mapStyle="mapbox://styles/mapbox/streets-v12"
      style={{ width: "100%", height: 400 }}
    >
      <NavigationControl position="top-right" />
      {stores.map((store) => (
        <Marker key={store.id} longitude={store.lng} latitude={store.lat}>
          <Pin />
        </Marker>
      ))}
    </Map>
  );
}
// 附近搜索的 Haversine 距离计算
function haversineDistance(lat1: number, lon1: number, lat2: number, lon2: number): number {
  const R = 6371; // 公里
  const dLat = ((lat2 - lat1) * Math.PI) / 180;
  const dLon = ((lon2 - lon1) * Math.PI) / 180;
  const a = Math.sin(dLat / 2) ** 2 + Math.cos((lat1 * Math.PI) / 180) * Math.cos((lat2 * Math.PI) / 180) * Math.sin(dLon / 2) ** 2;
  return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
}

避免

  • 服务端渲染地图组件 — 始终使用 ssr: false 或 "use client" 加载
  • 未经聚类直接渲染 1000+ 标记 — 会严重影响性能;请使用 supercluster
  • 硬编码坐标 — 在构建时或按需对地址进行地理编码
  • 忽略地理定位权限拒绝 — 始终提供回退方案(如手动输入地址)

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能