npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@lchhzz/open-map

v1.1.2

Published

一个支持随机选择多个地图服务(百度、高德、腾讯)进行逆地址解析的 TypeScript 库。

Downloads

567

Readme

@lchhzz/open-map

一个支持随机选择多个地图服务(百度、高德、腾讯)进行逆地址解析的 TypeScript 库。

特性

  • 🗺️ 支持百度地图、高德地图、腾讯地图的逆地址解析 API
  • 🎲 随机选择可用的地图服务,提高可用性和容错性
  • 🔧 支持多配置,每个地图服务可配置多个 API Key
  • 📦 TypeScript 支持,完整的类型定义
  • 🚀 简单易用的 API

安装

npm install @lchhzz/open-map

快速开始

1. 创建配置文件

运行初始化命令自动创建配置文件:

npm run mp:init

或者手动在项目根目录创建 openMap/openMap.config.ts 文件:

import { configDto } from '@lchhzz/open-map/src/type'

const config: configDto = {
  // 百度地图
  baiduMaps: [
    {
      ak: 'your-baidu-api-key',
      sn: 'your-baidu-secret-key' // 可选,如果需要签名验证
    }
  ],
  // 高德地图
  gaodeMap: [
    {
      ak: 'your-gaode-api-key'
    }
  ],
  // 腾讯地图
  tencentMap: [
    {
      ak: 'your-tencent-api-key',
      sn: 'your-tencent-secret-key' // 可选,如果需要签名验证
    }
  ]
}

export default config

或者使用 JavaScript 配置文件 openMap/openMap.config.js

module.exports = {
  baiduMaps: [
    { ak: 'your-baidu-api-key', sn: 'your-baidu-secret-key' }
  ],
  gaodeMap: [
    { ak: 'your-gaode-api-key' }
  ],
  tencentMap: [
    { ak: 'your-tencent-api-key', sn: 'your-tencent-secret-key' }
  ]
}

2. 使用示例

import openMaps from '@lchhzz/open-map'

// 创建实例
const maps = new openMaps()

// 随机选择一个地图服务进行逆地址解析
const result = await maps.tandomMap(39.908823, 116.397470)
console.log(result)

// 指定使用百度地图
const baiduResult = await maps.baiduMap(39.908823, 116.397470)
console.log(baiduResult)

// 指定使用高德地图
const gaodeResult = await maps.gaodeMap(39.908823, 116.397470)
console.log(gaodeResult)

// 指定使用腾讯地图
const tencentResult = await maps.tencentMap(39.908823, 116.397470)
console.log(tencentResult)

API 文档

openMaps

构造函数

const maps = new openMaps()

方法

tandomMap(latitude: number, longitude: number): Promise<MapResponse>

随机选择一个可用的地图服务进行逆地址解析。

参数:

  • latitude: 纬度
  • longitude: 经度

返回: Promise<MapResponse>

baiduMap(latitude: number, longitude: number): Promise<MapResponse>

使用百度地图进行逆地址解析。

参数:

  • latitude: 纬度
  • longitude: 经度

返回: Promise<MapResponse>

gaodeMap(latitude: number, longitude: number): Promise<MapResponse>

使用高德地图进行逆地址解析。

参数:

  • latitude: 纬度
  • longitude: 经度

返回: Promise<MapResponse>

tencentMap(latitude: number, longitude: number): Promise<MapResponse>

使用腾讯地图进行逆地址解析。

参数:

  • latitude: 纬度
  • longitude: 经度

返回: Promise<MapResponse>

响应格式

interface MapResponse {
  /** 是否成功 */
  state: boolean
  /** 错误信息 */
  error?: string
  /** 地址数据 */
  data?: {
    address: string        // 格式化地址
    province: string        // 省份
    city: string           // 城市
    district: string       // 区县
    street: string         // 街道
    raw: any              // 原始响应数据
  }
}

配置说明

配置文件位置

配置文件应放在项目根目录的 openMap 文件夹下,文件名可以是:

  • openMap.config.ts
  • openMap.config.js

配置格式

interface configDto {
  baiduMaps: Array<{
    ak: string    // API Key(必填)
    sn?: string   // Secret Key(可选,用于签名验证)
  }>
  gaodeMap: Array<{
    ak: string    // API Key(必填)
    sn?: string   // Secret Key(可选,通常不需要)
  }>
  tencentMap: Array<{
    ak: string    // API Key(必填)
    sn?: string   // Secret Key(可选,用于签名验证)
  }>
}

多配置支持

每个地图服务支持配置多个 API Key,系统会随机选择一个使用,这样可以:

  • 提高 API 调用配额
  • 实现负载均衡
  • 提高可用性

工具方法

ConfigManager.createSetting(config?: configDto)

创建默认配置文件。

import { ConfigManager } from '@lchhzz/open-map/src/utils/config'

// 创建默认配置文件
ConfigManager.createSetting()

获取 API Key

百度地图

  1. 访问 百度地图开放平台
  2. 注册账号并创建应用
  3. 获取 AK(Access Key)和 SK(Secret Key)

高德地图

  1. 访问 高德开放平台
  2. 注册账号并创建应用
  3. 获取 Key

腾讯地图

  1. 访问 腾讯位置服务
  2. 注册账号并创建应用
  3. 获取 Key 和 Secret Key

开发

# 安装依赖
npm install

# 初始化配置文件
npm run mp:init

许可证

ISC

贡献

欢迎提交 Issue 和 Pull Request!