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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pikaz/location

v1.0.10

Published

used to obtain location from the browser

Downloads

57,372

Readme

介绍

供中国地区使用的 js 定位插件

特性:

  • 支持浏览器 h5 定位、ip 定位、经纬度查询地址、ip 查询、地区编码查询地址、地址文本解析、省市区三级联动列表搜索
  • 定位信息文件已做压缩处理,如果有启用 gzip 时部分定位大约只会花费几十 k
  • web 端定位库,无需在服务端额外运行定位服务,没有并发限制、次数限制且无收费

ps:

  • 由于浏览器限制,http 域名的网页使用 h5 定位可能会出现问题,如定位不准、禁止定位等,如果想要定位结果更加精准,最好使用 https 域名;

  • 该插件的定位文件存放在第三方 cdn 中,若想存放至自己的 cdn 上,则可参考setConfig函数使用方法介绍

更新日志:

  • 0.1.x 文档请点击这里,1.x.x 版本相比 0.1.x 版本有破坏性更新,如需升级注意重新对接文档。
  • 相比 0.1.x 版本新增了 ip 查询地址、地址文本解析、地区编码查询地址。
  • 重构了 ip 定位方法,使用 ip 文件处理 ip 定位,对第三方的 ip 定位无依赖,ip 获取默认会缓存一天,也可自行结合自己服务获取用户 ip 进行 ip 定位。
  • 对定位文件进行优化,使每次定位所加载的文件更小,且对所有加载过的定位文件进行持久化缓存,第二次对于同市的定位速度在 100ms 以内。
  • 1.0.9 版本优化了 cdn 文件加载逻辑
  • 1.0.10 版本新增 esmodule 包,兼容 vite

demo 示例点击这里一键体验

demo 代码点击这里一键 copy

安装

with npm or yarn

yarn add @pikaz/location

npm i -S @pikaz/location
import { getLocation } from '@pikaz/location'

with cdn

<script
    type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/@pikaz/location"
></script>
或者
<script type="text/javascript" src="https://unpkg.com/@pikaz/location"></script>
const { getLocation } = window.pikazLocation

方法函数

注:detail开启则会在原本基础定位信息的基础上返回省市区的详细信息

方法示例

说明:设置全局配置(不调用则使用默认配置)

可把该项目的根目录下的 static 文件夹整个上传至您的 oss 上,将 static 文件夹的链接地址作为 url 传入,如 oss 上的 static 文件夹可通过https://xxx.com/file/static访问,则url可传入https://xxx.com/file,若不设置,则url默认使用https://cdn.jsdelivr.net/npm/@pikaz/location/lib等公共cdn地址(第三方cdn可能不稳定,最好自行上传定位文件)

import { setConfig } from '@pikaz/location'
setConfig({
    // 超时时间
    timeout: 10000,
    // 您的oss地址
    url: 'https://unpkg.com/@pikaz/location/lib',
})

说明:默认定位函数,优先使用 html5 定位,html5 定位失败,则会自动切换为 ip 定位

import { getLocation } from '@pikaz/location'
getLocation()
    .then((res) => {
        // 返回数据结构: {
        // ...,//返回对应定位类型数据,同下方的getH5Location和getIpLocation返回数据格式
        // type:"h5"//定位类型:h5/ip
        // }
    })
    .catch((err) => {
        console.log(err)
    })

说明:html5 定位函数,html5 定位推荐使用 https 协议,若为 http,则 html5 定位可能出现定位不准确或无法定位(取决于浏览器策略),开启高精度定位会更加耗时;

import { getH5Location } from '@pikaz/location'
getH5Location({
    // 开启gps高精度定位
    enableHighAccuracy: true,
})
    .then((res) => {
        // 返回数据结构:{
        //     address: "广东省深圳市福田区"//完整地址
        //     city: "深圳市"//市级名称
        //     code: "440304"//地区编码
        //     district: "福田区"//区县级名称
        //     latitude: 22.547//区县级纬度
        //     longitude: 114.085947//区县级经度
        //     province: "广东省"//省级名称
        // }
    })
    .catch((err) => {
        console.log(err)
    })

说明:ip 定位函数,ip 定位相比 html5 定位精度更差且可能不准,但是若用户拒绝定位授权时,则可以使用 ip 定位作为兜底方案;

import { getIpLocation } from '@pikaz/location'
getIpLocation()
    .then((res) => {
        // 返回数据结构:{
        //     address: "江苏省南京市"//完整地址
        //     city: "南京市"//市级名称
        //     code: "320100"//地区编码
        //     district: ""//区县级名称
        //     province: "江苏省"//省级名称
        //     ip: "114.114.114.114"//ip地址
        // }
    })
    .catch((err) => {
        console.log(err)
    })

说明:解析地址文本,必须带有省级或市级的全写或简写,不能只包含区县名称,如广东深圳/广东省深圳市/广东南山/深圳/深圳南山

import { searchAddress } from '@pikaz/location'
searchAddress({
    address: '广东福田',
})
    .then((res) => {
        // 返回数据结构:{
        //     address: "广东省深圳市福田区"//完整地址
        //     city: "深圳市"//市级名称
        //     code: "440304"//地区编码
        //     district: "福田区"//区县级名称
        //     province: "广东省"//省级名称
        // }
    })
    .catch((err) => {
        console.log(err)
    })

说明:地区编码查询地址信息

import { searchCodeAddress } from '@pikaz/location'
searchCodeAddress({
    code: '440304',
})
    .then((res) => {
        // 返回数据结构:{
        //     address: "广东省深圳市福田区"//完整地址
        //     city: "深圳市"//市级名称
        //     code: "440304"//地区编码
        //     district: "福田区"//区县级名称
        //     province: "广东省"//省级名称
        // }
    })
    .catch((err) => {
        console.log(err)
    })

说明:省市区三级联动,传入对应行政单位编码,获取下级行政单位列表;不传行政单位编码默认获取省级单位列表

import { searchList } from '@pikaz/location'
searchList({
    code: '110000',
})
    .then((res) => {
        // 返回数据结构: [
        // {
        //  "code":"110101",//该地区编码
        //  "name":"东城区",//该地区名称
        // },
        // {
        //  "code":"110102",//该地区编码
        //  "name":"西城区",//该地区名称
        // }
        // ]
    })
    .catch((err) => {
        console.log(err)
    })

说明:获取单个地区的详细信息:地区编码、经纬度、名称、名称拼音

import { searchCodeDetail } from '@pikaz/location'
searchCodeDetail({
    code: '440304',
})
    .then((res) => {
        // {
        //     "code": "440304", //该地区编码
        //     "location": { //该地区经纬度
        //         "latitude": 22.521541,
        //         "longitude": 114.05498
        //     },
        //     "name": "福田区", //该地区名称
        //     "pinyin": "futianqu" //该地区拼音
        // }
    })
    .catch((err) => {
        console.log(err)
    })

说明:若开启则会在原本的返回数据中额外返回详细的地址信息

import { getH5Location } from '@pikaz/location'
getH5Location({
    // 获取详细地址信息
    detail: true,
})
    .then((res) => {
        // {
        //     address: "广东省深圳市福田区" //完整地址
        //     city: "深圳市" //市级名称
        //     code: "440304" //地区编码
        //     district: "福田区" //区县级名称
        //     latitude: 22.547 //区县级纬度
        //     longitude: 114.085947 //区县级经度
        //     province: "广东省" //省级名称
        //     "detail": { //详细地址信息
        //         "province": {
        //             "code": "440000", //省级地区编码
        //             "location": { //省级地区经纬度
        //                 "latitude": 23.13171,
        //                 "longitude": 113.26627
        //             },
        //             "name": "广东省", //省级名称
        //             "pinyin": "guangdong" //省级名称拼音
        //         },
        //         "city": {
        //             "code": "440300", //市级地区编码
        //             "location": { //市级地区经纬度
        //                 "latitude": 22.54286,
        //                 "longitude": 114.05956
        //             },
        //             "name": "深圳市", //市级名称
        //             "pinyin": "shenzhen" //市级名称拼音
        //         },
        //         "district": {
        //             "code": "440304", //区县级地区编码
        //             "location": { //区县级地区经纬度
        //                 "latitude": 22.521541,
        //                 "longitude": 114.05498
        //             },
        //             "name": "福田区", //区县级名称
        //             "pinyin": "futianqu" //区县级名称拼音
        //         }
        //     }
    })
    .catch((err) => {
        console.log(err)
    })