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

@truckhome/global-position

v4.0.7

Published

卡家公共定位

Downloads

5

Readme

global-position(自有白名单可用)

文档

百度 api 官方文档 百度服务类 高德 api 官方文档 高德服务类 api

项目说明

全局定位统一脚本,pc(ip 定位),m 站(ios,高德定位、安卓,h5 gps 定位)、app(ios,app 定位、安卓,百度普通定位)

说明

  1. M 端定位-iOS 使用百度定位、Android 如果有 h5 原生定位使用 h5 定位,否则使用 ip 定位。
  2. 微信使用微信定位。如果没有声明,使用 M 端定位
  3. app 使用 UA 中的经纬度。如果没有经纬度,调用 app 定位方法
  4. pc 使用 ip 定位

项目运行

webpack5 + ts

pnpm i
pnpm dev

调用说明

// 默认数据
{
  "status": 0,
  "data": {
    "province": { "id": 0, "name": "全国", "enFirst": "" },
    "city": {
      "id": 0,
      "name": "",
      "sName": "",
      "enFirst": ""
    },
    "district": { "id": 0, "name": "" },
    "position": { "longitude": 0, "latitude": 0 }
  },
  "msg": "success"
}
npm i @truckhome/global-position
import { GlobalPosition } from '@truckhome/global-position'

// cdn地址:
<script src="https://unpkg.com/@truckhome/global-position@version/dist/global-position-umd.min.js"></script>


const g = GlobalPosition({
  immediate? // 默认false, 是否立即重新获取定位
  noCookie? // (后续会弃用)默认false, 是否不使用cookie 等同于immediate
  cookieTime? // 缓存时间
  ip? // 服务端使用该参数 传入客户端ip (ip获取经纬度接口会使用传入ip)
  success: (data, point) => {
    // 只有获取到城市信息才会执行
    console.log('先执行111', data)
  },
  error: (err, point, result) => {
    // 经纬度获取失败、ip定位失败、没有获取到城市信息都会执行
    // point 如果有就会返回没有就是 0 0
    // result基本是默认的数据结构。也可以通过实例上的result获取
    // g.result === result
    console.log('请求错误', err, point, result)
  }
})

// 或者
// successCallback -> data.success
// errorCallback -> data.error
const g = GlobalPosition(data?, successCallback, errorCallback)
const g = GlobalPosition(successCallback, errorCallback)

// 全局实例对象
window.globalPositionInstance === g
window.isDev = true // 是否开发环境

// 增加坐标转换方法
import { bdTranslate, bd2gd, gd2bd } from '@truckhome/global-position'

/**
 * point: {lng,lat}
 * from:
 *  1, GPS标准坐标
 * 2, 搜狗地图坐标
 *  3,火星坐标(gcj02),即高德、腾讯、MapABC等
 * 4, 3中列举的地图坐标对应的墨卡托平面坐标
 * 5, 百度地图采用的经纬度坐标(bd09)
 *  6,百度地图采用的墨卡托平面坐标
 * 7,图吧地图坐标
 *  8,51地图坐标
 *
 * to:
 * 3, 火星坐标(gcj02),即高德、腾讯地图及MapABC等
 * 5, 百度地图采用的经纬度坐标(bd09)
 * 6, 百度地图墨卡托平面坐标
 * callback: (data: {status: number, point: { lat: number; lng: number }) => void // status: 0 成功
 */
bdTranslate({lng,lat}, 1, 5, function(data){
  console.log(data)
})

bd2gd(lng, lat) -> {lng, lat}

gd2bd(lng, lat) -> {lng, lat}

描述

  1. 只调用一次接口返回城市信息,如果有结果了,直接返回结果
  2. cookie 缓存 5 分钟,如果有结果了,直接返回结果
  3. 客户端使用原生 xhr、服务端使用 axios 请求,需要 axios
<script>
  // ie 兼容 object.assign
  if (typeof Object.assign !== 'function') {
    Object.assign = function (target) {
      'use strict'
      if (target === null) {
        throw new TypeError('Cannot convert undefined or null to object')
      }
      target = Object(target)
      for (var index = 1; index < arguments.length; index++) {
        var source = arguments[index]
        if (source !== null) {
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key]
            }
          }
        }
      }
      return target
    }
  }
</script>
// 服务端使用需要全局axios
<script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>

更新时间

  • 2022-06-17

  • 2022-06-20

    新增 translatePoint 函数。修改 d.ts 生成方式

  • 2022-06-22

    兼容 ie promise

  • 2022-06-26

    兼容服务端使用 ip 定位

  • 2022-07-05

    传入 ip 使用传入的 ip 获取经纬度 在服务端使用的话,需要立即执行 immediate: true。否则服务端渲染就会保留定位信息在服务器

  • 2022-08-30

    兼容业务

  • 2022-09-06

    百度定位

  • 2022-10-12

    修改默认 errResult 到 errmsg

  • 2022-11-04

    修改 Android-m 端 gps 请求后,去除百度接口进行 point s 转换超限问题 gps 失败后通过 ip 定位

  • 2022-11-08

    修改 iOS-m 端,优先浏览器定位,失败后使用 ip 定位

  • 2022-11-10

    m 端 gps 失败后,使用 ip 定位。修改全局状态